Tweak Your WooCommerce Emails Using This Code

WooCommerce is an extremely powerful platform for adding e-Commerce functionality to your WordPress site.

From downloadable WooCommerce extensions to a full library of code snippets, you can tweak, extend and customize WooCommerce to your heart’s desire.

While most of the customizations deal with the e-Commerce platform itself, let’s talk about ways to customize one of the most important touchpoints for your customers — WooCommerce emails.

Email Template Customizations

The first way to tweak your WooCommerce emails is by simply overwriting the email templates themselves.

They’re all located in:

/woocommerce/templates/emails/

By copying the individual file into your theme’s WooCommerce folder, you can ensure that any changes you make are used instead of the default code.

A forewarning: overwriting template files may make them out-of-date when WooCommerce releases an update, so you may need to make your edits again and again. That’s reason enough to keep the updates minimal.

Thankfully, the WooCommerce developers are good enough to let you know which of your template files are out-of-date, so you can easily see what needs to be updated when there’s a new version out.

Here’s a customization tip: updating styles in emails is incredibly annoying. Add CSS in the head section of the email-header.php file so they’re used in every email for consistency.

Hooking into Email Actions and Filters

The better (and more future-proof) way to customize your WooCommerce emails is to hook into actions and filters to add and change content where you need it.

WooCommerce doesn’t do a great job of listing out email-only actions and filters, but here’s a few listed in the documentation:

woocommerce_email_after_order_table
woocommerce_email_before_order_table
woocommerce_email_customer_details
woocommerce_email_footer
woocommerce_email_header
woocommerce_email_order_details

Here’s a practical example (which we actually use with a client): including any coupons used in the email sent to the shop after an order.

add_action( 'woocommerce_email_after_order_table', 'add_coupon_to_admin_new_order', 15, 2 );

function add_coupon_to_admin_new_order( $order, $is_admin_email ) {
	
	if ( $is_admin_email ) {
	
		if( $order->get_used_coupons() ) {
		
			$coupons_count = count( $order->get_used_coupons() );
		    
		    $i = 1;
		    $coupons_list = '';
		    
		    foreach( $order->get_used_coupons() as $coupon) {
		        $coupons_list .=  $coupon;
		        if( $i < $coupons_count )
		        	$coupons_list .= ', ';
		        $i++;
		    }
		
		    echo '<p><strong>Discount used (' . $coupons_count . ') :</strong> ' . $coupons_list . '</p>';
		
		} // endif get_used_coupons
	
	} // endif $is_admin_email
}

If you have custom fields that you’re saving with every order, then use the filter hook ‘woocommerce_email_order_meta_fields’ to add your custom fields to emails.

The WooCommerce Docs include a great example with code of adding a custom field to your 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;
}

Changing and Testing Emails

WooCommerce has a number of built-in emails, but you may not need or want to send all of them.

To see the list of emails, go to WooCommerce -> Settings -> Emails.

To turn off an email, simply click the Gear icon to configure and uncheck the “Enable this email notification” box. (This is also where you can easily see if the email’s subject line, the email type (HTML, text or multipart) and if you’re currently overwriting the default email in your theme.

You can also remove or unhook WooCommerce emails in your functions file, including store event emails like low- or out-of-stick notifications.

One of the handiest tools is the WooCommerce Email Test plugin, which lets you preview your emails to see any changes you’ve made. It’s super handy when making little tweaks, so you don’t have to do an order every time you make a change.

You can preview New Order emails, Customer Invoice emails, and more. With the Premium version of the plugin, you can even have email previews sent to your inbox.

Photo by timothy muza on Unsplash

Jason Unger
About Jason Unger

Jason Unger is the Founder of Digital Ink, the creative and digital team that builds brands and helps companies grow. Based outside of Washington, D.C., Jason has done it all, from website strategy, design, development, troubleshooting, maintenance, content and marketing.