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