Navigation Menu

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;
}
@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