Add Order Meta Fields to WooCommerce Emails

For now it is the last post in the series about WooCommerce order meta. We began with checkout page, then we added order meta in WordPress admin, and to “Thank you” and “View Order” pages. Now it is time for emails.

/9 comments

On the screenshot below you can see that the additional “Gift Information” section has been added. We already worked with this fields in previous posts in this series.

WooCommerce add additional section with order meta fields to emails.
WooCommerce Processing Order email with Default Email Theme.

If you add the below code to your functions.php, the “Gift” section will be added mostly for all default WooCommerce emails, except “New Account” and “Reset Password” emails.

add_action( 'woocommerce_email_order_meta', 'misha_add_email_order_meta', 10, 3 );
/*
 * @param $order_obj Order Object
 * @param $sent_to_admin If this email is for administrator or for a customer
 * @param $plain_text HTML or Plain text (can be configured in WooCommerce > Settings > Emails)
 */
function misha_add_email_order_meta( $order_obj, $sent_to_admin, $plain_text ){
 
	// this order meta checks if order is marked as a gift
	$is_gift = get_post_meta( $order_obj->get_order_number(), 'is_gift', true );
 
	// we won't display anything if it is not a gift
	if( empty( $is_gift ) )
		return;
 
	// ok, if it is the gift order, get all the other fields
	$gift_wrap = get_post_meta( $order_obj->get_order_number(), 'gift_wrap', true );
	$gift_recipient = get_post_meta( $order_obj->get_order_number(), 'gift_name', true );
	$gift_message = get_post_meta( $order_obj->get_order_number(), 'gift_message', true );
 
 
	// ok, we will add the separate version for plaintext emails
	if ( $plain_text === false ) {
 
		// you shouldn't have to worry about inline styles, WooCommerce adds them itself depending on the theme you use
		echo '<h2>Gift Information</h2>
		<ul>
		<li><strong>Is gift?</strong> Yes</li>
		<li><strong>Gift Wrap:</strong> ' . $gift_wrap . '</li>
		<li><strong>Recipient name:</strong> ' . $gift_recipient . '</li>
		<li><strong>Gift message:</strong> ' . wpautop( $gift_message ) . '</li>
		</ul>';
 
	} else {
 
		echo "GIFT INFORMATION\n
		Is gift: Yes
		Gift Wrap: $gift_wrap
		Recipient name: $gift_recipient
		Gift message: $gift_message";	
 
	}
 
}

Each WooCommerce email can be configured to be sent and displayed as a plain text email. Lines 23 and 34–42 in the above code are just for it.

WooCommerce New Order plain text email with Meta Fields we added.

There is another way to do it. I don’t like it but you can keep it in mind

If you look through official WooCommerce tutorials and some other tutorials over the internet, you will find out that people use woocommerce_email_order_meta_fields and woocommerce_email_order_meta_keys filter hooks.

I do not recommend it because woocommerce_email_order_meta_keys is deprecated since 2.3.0. Filter woocommerce_email_order_meta_fields adds the fields correctly but the ugly way. On the screenshot below is what I mean:

That is how it looks when you add order meta to emails with woocommerce_email_order_meta_fields filter hook.
That’s how it looks when you add order meta to emails with woocommerce_email_order_meta_fields filter hook.

But for plain text emails and some other email themes everything looks good. Of course you can increase the top margin with CSS but this way doesn’t look so good. Anyway, I want to share the code with you:

add_filter('woocommerce_email_order_meta_fields', 'misha_add_email_order_meta_fields', 10, 3 );
function misha_add_email_order_meta_fields( $fields, $sent_to_admin, $order_obj ) {
 
	$is_gift = get_post_meta( $order_obj->get_order_number(), 'is_gift', true );
 
	// we won't display anything if it is not a gift
	if( empty( $is_gift ) )
		return $fields;
 
	$fields['is_gift'] = array(
		'label' => 'Is gift?',
		'value' => 'Yes'
	);
 
	$fields['gift_wrap'] = array(
		'label' => 'Gift Wrap',
		'value' => get_post_meta( $order_obj->get_order_number(), 'gift_wrap', true )
	);
 
	$fields['gift_name'] = array(
		'label' => 'Recipient name',
		'value' => get_post_meta( $order_obj->get_order_number(), 'gift_name', true )
	);
 
	$fields['gift_message'] = array(
		'label' => 'Gift message',
		'value' => wpautop( get_post_meta( $order_obj->get_order_number(), 'gift_message', true ) )
	);
 
	return $fields;
 
}

By the way, this filter can be useful when you want to unset() some of fields that were set by a plugin.

More about Order Meta

Misha Rudrastyh

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 10 yrs of experience.

Need some custom developer help? Let me know

Follow Misha

Need some help with WooCommerce?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Comments — 9

Leave a comment

php js HTML CSS Code