skip to Main Content
Custom  Customer Processing Order Message

Sometimes you need to add a custom Customer Processing Order message to your email template for your clients. This so you send them additional instructions. In WooCommerce the basic email editing options are rather unlimited. Not to worry though. There are options.

Initial Trial

You can add the following action hook to the functions.php file of your theme or child theme:

add_action( 'woocommerce_email_before_order_table','add_order_email_instructions', 10, 4 );
function add_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {
    if ( 'customer_processing_order' == $this->id && ! $sent_to_admin ) {
        echo 'Your login details are on their way. (They may take up to 10 minutes to arrive in your inbox.';
    } 
}

Snippet based on Helga the Viking’s code at SO.

Details

It (the code snippet / action hook) tells WooCommerce to add an extra Custom Customer Processing Order Message to the customer processing order email.You could just add the message to all outgoing emails by WooCommerce and drop the if statement. Or you could go for another email template where you would like to add code to. It is all possible really.

NB The actual action is added to email-order-details.php as well as some other payment gateway files.

Object Snag

With the mentioned action you will however get this error:

2018-02-19T22:31:46+00:00 CRITICAL Uncaught Error: Using $this when not in object context in /srv/www/domain.com/releases/20180218085005/web/app/themes/jupiter-child/functions.php:64
Stack trace:
#0 /srv/www/domain.com/releases/20180218085005/web/wp/wp-includes/class-wp-hook.php(286): add_order_email_instructions(Object(WC_Order), true, false, Object(WC_Email_New_Order))
#1 /srv/www/domain.com/releases/20180218085005/web/wp/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array)
#2 /srv/www/domain.com/releases/20180218085005/web/wp/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#3 /srv/www/domain.com/releases/20180218085005/web/app/plugins/woocommerce/templates/emails/email-order-details.php(24): do_action('woocommerce_ema...', Object(WC_Order), true, false, Object(WC_Email_New_Order))
#4 /srv/www/domain.com/releases/20180218085005/web/app/plugins/woocommerce/includes/wc-core-functions.php(211): include('/srv/www/publiq...')
#5 /srv/www/domain.com/releases/20180218085005/web/app/plugins/woocommerce/includes/

This is because  you can’t use $this outside of the class definition. But with the $email object added this should not occur. And that is available since WooCommerce 2.5+. You just need it as a parameter too. Well, as you will see things changed somewhat in WC 3+ and another object->property is needed.

WooCommerce 3+ Updated Trial

So with WooCommerce 3+ I then thought I needed:

add_action( 'woocommerce_email_before_order_table','add_order_email_instructions', 10, 4 );
function add_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {
if ( 'customer_processing_order' == $order->get_id() && ! $sent_to_admin ) {
echo 'Your login details are on their way. (They may take up to 10 minutes to arrive in your inbox.';
}
}

with $order->get_id() as I otherwise still had the same error. But the if statement did not do the trick properly.

Working Solution

The final best option is with changed if statement:

add_action( 'woocommerce_email_before_order_table','add_order_email_instructions', 10, 4 );
function add_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id == 'customer_processing_order' ) {
echo 'Your login details are on their way. (They may take up to 10 minutes to arrive in your inbox.';
}
}

So $email->id == ‘customer_processing_order works. Result:

Order being processed custom message

Advertisements

Jasper Frumau

Jasper has been working with web frameworks and applications such as Laravel, Magento and his favorite CMS WordPress including Roots Trellis and Sage for more than a decade. He helps customers with web design and online marketing. Services provided are web design, ecommerce, SEO, content marketing. When Jasper is not coding, marketing a website, reading about the web or dreaming the internet of things he plays with his son, travels or run a few blocks.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top