Skip to content

Instantly share code, notes, and snippets.

@woogists
Last active September 23, 2021 20:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save woogists/1b71d6842960fa056bd61bd0ca60ecba to your computer and use it in GitHub Desktop.
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;
}
@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