Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce - Add custom field (in an order) to the order emails
/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');
function my_woocommerce_email_order_meta_keys( $keys ) {
$keys['How did you hear about us?'] = 'hear_about_us';
return $keys;
}
@Ramoonus

This comment has been minimized.

Copy link

commented Mar 31, 2012

roughly the same as https://gist.github.com/1547491

@mikejolley

This comment has been minimized.

@Ramoonus

This comment has been minimized.

Copy link

commented Mar 31, 2012

whereas $keys = array ?

@fabio-farolfi

This comment has been minimized.

Copy link

commented Aug 9, 2013

I purchased Checkout Field Editor and I added a new field. I see him during the purchase, but does not appear in the mail: http://i.imgur.com/owwD5S7.jpg

I used this code:

add_filter('woocommerce_email_order_meta_keys', 'billing_cfiscale');

function billing_cfiscale( $keys ) {
$keys[] = 'Codice Fiscale';
return $keys;
}

@seozones

This comment has been minimized.

Copy link

commented Sep 30, 2013

Hi there. I purchased this plugin today (25 sites). I installed it and added in an extra field (label is Ship Date) under Shipping. I also added in the script above so that the new field would be added to the emails going out. However, adding in the above script did not add it to any email order receipt nor email to the admin. Please help.

@designloud

This comment has been minimized.

Copy link

commented Oct 20, 2013

@seozones and @fabio-farolfi make sure you are using the name and not the label as the key in the above code.

For example if I have a checkout field named my_example with label of "My Example" then for the fields to show in the admin emails the code would look like this:

/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
 
function my_custom_checkout_field_order_meta_keys( $keys ) {
$keys[] = 'my_example';
return $keys;
}

That is how I got it to work for me ;) Cheers!

@corsonr

This comment has been minimized.

Copy link

commented Oct 21, 2013

I confirm the snippet works, but you have to use the field slug, not the label or the name.

@woods950

This comment has been minimized.

Copy link

commented Oct 23, 2013

Thanks to everyone for helping with this addition. Question I have, how do I add more than one custom field?

@woods950

This comment has been minimized.

Copy link

commented Oct 23, 2013

Found my own answer after doing a web search. Found the necessary info here: http://bit.ly/H0VPP4

@sfplanet

This comment has been minimized.

Copy link

commented Apr 10, 2014

Does still work for woocoommerce 2.1.6? I can't seem to get it working. Here's my code

//display custom fields backend product creation
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields');

function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '

';
woocommerce_wp_text_input(
array(
'id' => 'amazon_code',
'label' => __('Amazon Coupon Code', 'woocommerce'),
'placeholder' => 'Enter Coupon Code',
'desc_tip' => 'true',
'description' => __('Enter Amazon Coupon Code', 'woocommerce')
)
);

echo '</div>';

}
//save fields from product backend creation
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save');
function woo_add_custom_general_fields_save($post_id) {
$woocommerce_text_field = $_POST['amazon_code'];
if(!empty($woocommerce_text_field))
update_post_meta($post_id, 'amazon_code', esc_attr($woocommerce_text_field));

}

//add custom amazon coupon code to email
add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');
function my_woocommerce_email_order_meta_keys($keys) {
$keys[] = 'amazon_code';
return $keys;
}

@devworkz-zz

This comment has been minimized.

Copy link

commented Jul 29, 2014

The code is not works for WC 2.1.12

@studio-fs

This comment has been minimized.

Copy link

commented Jan 27, 2017

woocommerce_email_order_meta_keys - not properly!!!
woocommerce_email_customer_details_fields - properly!
//
add_filter('woocommerce_email_customer_details_fields', 'my_woocommerce_email_order_meta_keys', 10, 1);
function my_woocommerce_email_order_meta_keys($keys) {
$keys[] = 'you're text';
return $keys;
}

@VegaMaxine

This comment has been minimized.

Copy link

commented Mar 22, 2017

Can anyone see a problem with this? Any assistance much appreciated!

add_filter('woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['delivery_date'] = array(
'label' => __( 'Pre-order' ),
'value' => get_post_meta( $order->id, 'delivery_date', true ),
);
$fields['message'] = array(
'label' => __( 'Message' ),
'value' => get_post_meta( $order->id, 'message', true ),
);
$fields['shipping_email'] = array(
'label' => __( 'Recipient Email' ),
'value' => get_post_meta( $order->id, 'shipping_email', true ),
);
$fields['delivery_instructions'] = array(
'label' => __( 'Special Delivery Instructions' ),
'value' => get_post_meta( $order->id, 'delivery_instructions', true ),
);
return $fields;
}

@emajen

This comment has been minimized.

Copy link

commented Jul 21, 2017

woocommerce_email_customer_details_fields doesn't work. I understand that woocommerce_email_order_meta_keys is depreciated, but it works...

@dimbert82

This comment has been minimized.

Copy link

commented Jun 1, 2018

Hi guys,

How can I implement this code to send the custom field in the "Admin New Order Email"?

@o-nkar

This comment has been minimized.

Copy link

commented Jul 4, 2018

@dimbert82
insert the code in your active theme or child theme functions.php

code like this:

add_filter( 'woocommerce_email_order_meta_fields', 'custom_order_mail_fields_callback', 10, 3 );
function custom_order_mail_fields_callback( $fields, $sent_to_admin, $order ) {    
        $fields['yourdata'] = array(
            'label' => __( 'yourdata to display' ),
            'value' => ' yourdata value',
        );
    }
    return $fields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.