WooCommerce: Send Default Order Email For Custom Status Transition

WooCommerce offers flexibility with custom order statuses, but email notifications can be limited. By default, in fact, WooCommerce triggers the “completed”, “processing”, “on-hold” and all the other default emails when the order status changes from X to Y as defined in the plugin code.

But what if you want to trigger the “Order Completed” email if the order status transitions from your custom order status to the “completed” one? Or what if you want to trigger the “Order On-hold” email beyond its default triggers?

This post unlocks a powerful technique: triggering a default order email for a custom order transition. Let’s see how it’s done!

Default WooCommerce Order Email Triggers

WC_Email_Cancelled_Order

As you can see here, the “cancelled order” email is only sent when the order goes from “processing” or “on-hold” to cancelled.

add_action( 'woocommerce_order_status_processing_to_cancelled_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_on-hold_to_cancelled_notification', array( $this, 'trigger' ), 10, 2 );

WC_Email_Customer_Completed_Order

The “Customer Completed” notification triggers every time the order goes to “completed”:

add_action( 'woocommerce_order_status_completed_notification', array( $this, 'trigger' ), 10, 2 );

WC_Email_Customer_On_Hold_Order

add_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_failed_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_cancelled_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );

WC_Email_Customer_Processing_Order

add_action( 'woocommerce_order_status_cancelled_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_on-hold_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ), 10, 2 );

WC_Email_Failed_Order

add_action( 'woocommerce_order_status_pending_to_failed_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_on-hold_to_failed_notification', array( $this, 'trigger' ), 10, 2 );

WC_Email_New_Order

add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_pending_to_completed_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_failed_to_completed_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_failed_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_cancelled_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_cancelled_to_completed_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_cancelled_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );

PHP Snippet 1: Trigger “Cancelled Order” Email When Order Status Goes From “pending” to “cancelled”

/**
 * @snippet       Send Cancelled Email If Previous Order Status Was "pending"
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_order_status_pending_to_cancelled', 'bbloomer_pending_to_cancelled_email' );

function bbloomer_pending_to_cancelled_email( $order_id ) {
   $mailer = WC()->mailer()->get_emails();
	$mailer['WC_Email_Cancelled_Order']->trigger( $order_id );
}

The code utilizes the add_action function provided by WordPress.

This function registers a new function, bbloomer_pending_to_cancelled_email, to be executed when a specific action occurs within WooCommerce.

In this case, the action we’re hooking into is woocommerce_order_status_pending_to_cancelled. This action is triggered whenever an order’s status transitions from ‘pending‘ to ‘cancelled‘.

Inside the function, WC()->mailer()->get_emails() retrieves an instance of the WC_Mailer class. This class is responsible for handling all email notifications sent by WooCommerce. By calling get_emails(), we get an array containing all the different email notification classes available in WooCommerce.

Finally, the code calls the trigger method on the retrieved email class (‘WC_Email_Cancelled_Order’). This method is what initiates the process of sending the default “Cancelled Order” email notification using the provided $order_id to populate the email content with relevant order details.

All you have to do is select the correct hook, and the email class you want to send. Here’s another example.

PHP Snippet 2: Trigger “Processing Order” Email When Order Status Goes From “custom_status” to “processing”

If you added a custom order status, you may want to trigger the “processing” email (or any default email) whenever the order status goes from your custom status (we’ll use “custom_status” as a placeholder) to a default status for example.

/**
 * @snippet       Send Processing Email If Previous Order Status Was "custom_status"
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_order_status_custom_status_to_processing', 'bbloomer_custom_status_to_processing_email' );

function bbloomer_custom_status_to_processing_email( $order_id ) {
   $mailer = WC()->mailer()->get_emails();
	$mailer['WC_Email_Customer_Processing_Order']->trigger( $order_id );
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Add Content to a Specific Order Email
    Customizing WooCommerce emails via the WordPress dashboard is not easy and – sometimes – not possible. For example, you can’t edit or add content to them unless you’re familiar with code. Well, here’s a quick example to learn how to add content to any WooCommerce default order email. In this case study, our goal is […]
  • WooCommerce: How to Add a Custom Checkout Field
    Let’s imagine you want to add a custom checkout field (and not an additional billing or shipping field) on the WooCommerce Checkout page. For example, it might be a customer licence number – this has got nothing to do with billing and nothing to do with shipping. Ideally, this custom field could show above the […]
  • WooCommerce Visual Hook Guide: Emails
    WooCommerce customizers: the Visual Hook Guide is back! Here’s a visual HTML hook guide for the WooCommerce Emails. This visual guide belongs to my “Visual Hook Guide Series“, that I’ve put together so that you can find WooCommerce hooks quickly and easily by seeing their actual locations. Let me know in the comments if this […]
  • WooCommerce: How to Add a Custom Order Status
    All WooCommerce orders go to either “processing“, “completed“, “on-hold” and other default order statuses based on the payment method and product type. Sometimes these statuses are not enough. For example, you might need to mark certain orders in a different way for tracking, filtering, exporting purposes. Or you might want to disable default emails by […]
  • WooCommerce: Add To: Cc: Bcc: Email Recipients
    The WooCommerce Email Settings allow you to add custom recipients only for New Order, Cancelled Order, Failed Order and all admin-only emails. But what if you want to add an email recipient to a customer email e.g. the Completed Order one? For example, you need to send it to your dropshipper. Also, you might want […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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