Insert a new field's data into new order notify email

Hi guys, I'm using the below code snippet I found online to add an extra field to the checkout form to ask customers how they discovered us. I notice that this field is not getting inserted into the emails generated by  Email Customizer.  Do you know how I can have it so that this new field gets inserted into the new order notification emails that I receive?  I should probably mention that I'm not a coder, I didn't generate the code below. I found it on a blog somewhere but it does what it's supposed to do. Thank you.

/**

* Add how did you discover us field to the 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 how you discovered us?'),

'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', 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>'.__('My Field').':</strong> ' . get_post_meta( $order->id, 'My Field', true ) . '</p>';

}

Asked on November 27, 2016 in Email Customizer.
' data-html="true"> Share
  • Comment(0)
  • Add Comment
    8 Answer(s)

    See if this maybe helps you:

    At the bottom... [ec_custom_field] allows you to create custom field shortcodes.
    Thanks
    Answered on November 30, 2016.
    ' data-html="true"> Share
  • Comment(0)
  • Add Comment

    It probably does, but I'm not much of a code person.  Your example shows: [ec_custom_field key=”_billing_company”]

    For my example code above, what would be the key that I put in the quotes?

    Answered on November 30, 2016.
    ' data-html="true"> Share
  • Comment(0)
  • Add Comment

    Hi,

    I've modified your code a little to make sure the KEY is better formatted, with underscores and no spaces- so the KEY is not "My Field" and is rather "my_field_name"

     /**
     * Add how did you discover us field to the 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 how you discovered us?'),
     '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>'.__('My Field').':</strong> ' . get_post_meta( $order->id, 'my_field_name', true ) . '</p>';
     }
     

     

    Then you can use our shortcode like this

     [ec_custom_field key="my_field_name"]
     

     

    Hope this helps 🙂

    Answered on December 5, 2016.
    ' data-html="true"> Share
  • Comment(0)
  • Add Comment

    I have no idea what you just said in your reply.  I see my_field_name in both examples and I also see My Field in both examples.

    Totally lost?? I don't think the code in my functions.php file is wrong, because it does save the field to the order when I view the order in the admin area.  I just want to be able to have that information also appear in the new order email that comes to me. Is that possible to do?  Which lines did you change? Do I need to replace the code in my functions file with yours? 

    Answered on December 5, 2016.
    ' data-html="true"> Share
  • Comment(0)
  • Add Comment

    Hi,

    This function has what we call a key in it:

    update_post_meta( $order_id, 'my_field_name', sanitize_text_field( $_POST['my_field_name'] ) );

    It should not be like this:

    update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field_name'] ) );

    But the answer is yes, you can copy and paste to replace the code in your functions.php, then you will be able to use our shortcode.

    [ec_custom_field key="my_field_name"]

    Answered on December 6, 2016.
    ' data-html="true"> Share
  • Comment(0)
  • Add Comment

    I added the code. Question - does the new code get rid of any existing custom fields that were previously saved to orders? I just went looking in a bunch of recent orders and couldn't find a single one with the custom field in the woocommerce order viewer.

    Answered on December 6, 2016.
    ' data-html="true"> Share
  • Comment(0)
  • Add Comment

    Yes, the new code will discard the existing custom fields.

    Don't worry tho, the data is all still there, and it will be visible again if you simply revert to the initial code you had posted here. I didn't realize this code was live yet.

    Considering it is live already, and you have already collected data with the existing code, then you can proceed with the key as it was - 'My Field'

    Then you should use our shortcode like this - [ec_custom_field key="My Field"]

    In future try to name keys more specifically and with underscores - e.g. 'My Field' could be named 'how_did_discover'. but don't worry about that this time. just use the code as you had it.

    Answered on December 7, 2016.
    ' data-html="true"> Share
  • Comment(0)
  • Add Comment

    Ok, thank you for your help 🙂

    Answered on December 7, 2016.
    ' data-html="true"> Share
  • Comment(0)
  • Add Comment

    Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.