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>'; }
See if this maybe helps you:
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?
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 🙂
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?
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"
]
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.
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.