Skip to content

Instantly share code, notes, and snippets.

@woogists
Last active September 23, 2021 20:53
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save woogists/1b71d6842960fa056bd61bd0ca60ecba to your computer and use it in GitHub Desktop.
Add a custom field (in an order) to the emails
/**
* Add a custom field (in an order) to the emails
*/
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['meta_key'] = array(
'label' => __( 'Label' ),
'value' => get_post_meta( $order->id, 'meta_key', true ),
);
return $fields;
}
@tiff7
Copy link

tiff7 commented Jan 31, 2020

So would it look like this?

/**
 * Add a custom field (in an order) to the emails
 */

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['meta_key'] = array(
        'label' => __( 'MY CUSTOM FIELD' ),
        'value' => get_post_meta( $order->id, 'MY_CUSTOM_FIELD', true ),
    );
    return $fields;
}

@markdh1
Copy link

markdh1 commented Feb 17, 2020

So would it look like this?

/**
 * Add a custom field (in an order) to the emails
 */

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['meta_key'] = array(
        'label' => __( 'MY CUSTOM FIELD' ),
        'value' => get_post_meta( $order->id, 'MY_CUSTOM_FIELD', true ),
    );
    return $fields;
}

Would be nice if we got an answer for this.

@Zimo92
Copy link

Zimo92 commented Apr 5, 2020

Indeed!

@conschneider
Copy link

Howdy,

The meta_key needs to match. So like this:

/**
 * Add a custom field (in an order) to the emails
 */

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['MY_CUSTOM_FIELD'] = array(
        'label' => __( 'MY CUSTOM FIELD' ),
        'value' => get_post_meta( $order->id, 'MY_CUSTOM_FIELD', true ),
    );
    return $fields;
}

@Zimo92
Copy link

Zimo92 commented Apr 21, 2020

Great! Do you know where this code has to be placed? Do we need to create a new .php and place it in the child theme folder? Or does it need to be added to another existing file?
Thanks

@Zimo92
Copy link

Zimo92 commented Apr 21, 2020

Ok, I found the solution it needs to be in functions.php, paste it to the end of the file.

@conschneider
Copy link

Hi,

Ok, I found the solution it needs to be in functions.php, paste it to the end of the file.

Correct. Cheers for adding that.

@wilburforce83
Copy link

I'm pretty green with PHP, but I know JS, when it comes to multiple extra fields, should I just copy, paste and rename the function i.e. custom_woocommerce_email_order_meta_fields or is there a loop function I can use. thanks!

@aquesada001
Copy link

What would this code look like if you had multiple fields to add?

@conschneider
Copy link

conschneider commented Jul 28, 2020

What would this code look like if you had multiple fields to add?

From what I see there is a foreach loop that processes the filter so you can pass multiple arrays.

/**
 * Add a custom field (in an order) to the emails
 */
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['meta_key_1'] = array(
        'label' => __( 'Label' ),
        'value' => get_post_meta( $order->id, 'meta_key_1', true ),
    );
    $fields['meta_key_2'] = array(
        'label' => __( 'Label' ),
        'value' => get_post_meta( $order->id, 'meta_key_2', true ),
    );
    return $fields;
}

Code is untested 🤞.

@helgatheviking
Copy link

I think this should also be updated for CRUD, which would be anything over WooCommerce 3.0. I've also added internationalization.

/**
 * Add a custom field (in an order) to the emails
 *
 * @param array $fields The meta fields.
 * @param bool  $sent_to_admin If should sent to admin.
 * @param WC_Order $order Order instance.
 * @return array
 */
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    $fields['meta_key_1'] = array(
        'label' => esc_html__( 'Meta Label 1', 'your-text-domain' ),
        'value' => $order->get_meta( 'meta_key_1', true ),
    );
    $fields['meta_key_2'] = array(
        'label' => esc_html__( 'Meta Label 2', 'your-text-domain' ),
        'value' => $order->get_meta( 'meta_key_2', true ),
    );
    return $fields;
}
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );

@conschneider
Copy link

👍 💌
Thanks a lot @helgatheviking 🙂!

@vicarh
Copy link

vicarh commented Jan 3, 2021

Has anyone actually verified this piece of code?

@marketingsolution
Copy link

Do you know how to move the field in the e-mail? Like I want it to be after the billing details and add some style

instead of just those fields?

@helgatheviking
Copy link

@marketingsolution I have a snippet in my tutorial: https://www.kathyisawesome.com/woocommerce-customize-checkout-fields/

This should display fields on the woocommerce_email_customer_details hook. You can check the email templates for available hooks.

function kia_display_email_order_meta( $order, $sent_to_admin, $plain_text ) { 
	$some_field = $order->get_meta( '_some_field' ); 
	$another_field = $order->get_meta( '_another_field' ); 
	if( $plain_text ){ 
		echo 'The value for some field is ' . $some_field . ' while the value of another field is ' . $another_field;
	} else { 
		echo 'The value for <strong>some field</strong> is ' . $some_field. ' while the value of <strong>another field</strong> is ' . $another_field . ';
	} 
} 
add_action('woocommerce_email_customer_details', 'kia_display_email_order_meta', 30, 3 );

@marketingsolution
Copy link

Thank you I dont know for what we are using this else { } ?
I try to adapt it but've got critical error on page, please take a look, want to show just one field txt:
`function NIP_display_email_order_meta( $order, $sent_to_admin, $plain_text ) {
$NIP = $order->get_meta( '_NIP' );

if( $plain_text ){ 
	echo 'Podany NIP to ' . $NIP;
} else { 
	echo 'The value for <strong>some field</strong> is ' . $NIP.;
} 

}
add_action('woocommerce_email_customer_details', 'NIP_display_email_order_meta', 30, 3 );
`

@helgatheviking
Copy link

@marketingsolution the if/else is for emails that are sent in plain text versus emails that are sent with HTML markup. You didn't say what your fatal error was so it's hard to say... though I do see a problem at the end of echo 'The value for <strong>some field</strong> is ' . $NIP.; there's a period just before the semi-colon that should not be there.

function NIP_display_email_order_meta( $order, $sent_to_admin, $plain_text ) {
  $NIP = $order->get_meta( '_NIP' );
  
  if( $plain_text ){ 
	  echo 'Podany NIP to ' . $NIP;
  } else { 
	  echo 'The value for <strong>some field</strong> is ' . $NIP;
  } 

}
add_action('woocommerce_email_customer_details', 'NIP_display_email_order_meta', 30, 3 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment