Support » Plugin: Kadence WooCommerce Email Designer » Include checkout field in new order email?

  • I’m using the following code snippet to add a checkout field to woocommerce. Is there a way to get the customer’s answer included in the new order email? Thank you!

    /**
     * ADD HOW DID YOU DISCOVER FIELD ON CHECKOUT
     */
     add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
     function my_custom_checkout_field( $checkout ) {
    echo '<div id="my_custom_checkout_field"><h3>' . __('How did you discover us?') . '</h3>';
    woocommerce_form_field(
     'my_field_name',
     array(
     'type' => 'text',
     'class' => array('my-field-class form-row-wide'),
     'label' => __('Please take a moment to tell us how you discovered our products?'),
     'placeholder' => __(''),
     ),
     $checkout->get_value( 'my_field_name' )
     );
    echo '</div>';
     }
    /**
     * Update the order meta with field value
     */
     add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
     function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['my_field_name'] ) ) {
     update_post_meta( $order_id, 'my_field_name', sanitize_text_field( $_POST['my_field_name'] ) );
     }
     }
    /**
     * Display field value on the order edit page
     */
     add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
     function my_custom_checkout_field_display_admin_order_meta( $order ){
    echo '<p><strong>'.__('How did you discover?').':</strong> ' . get_post_meta( $order->id, 'my_field_name', true ) . '</p>';
     }
    
Viewing 1 replies (of 1 total)
  • Could be like this:

     add_action( 'woocommerce_email_order_meta_fields', 'add_custom_woo_email_fields', 10, 3 );
    function add_custom_woo_email_fields( $keys, $sent_to_admin, $order ) {
    
    if ( ! is_object( $order ) ) {
    return $keys;
    }
    $order_id = $order->get_id();
    $custom_keys = array();
    $value = get_post_meta( $order_id, 'my_field_name', true );
    if ( ! empty( $value ) ) {
      $custom_keys[ 'my_field_name' ] = array(
         'label' => 'How did you discover?',
         'value' => $value,
      );
    }
    return array_merge( $keys, $custom_keys );
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Include checkout field in new order email?’ is closed to new replies.