How to Send Email on Custom Order Status in WooCommerce?

An e-commerce store includes many purposeful elements. WooCommerce order status is one such element. It informs the owner regarding the current order status. In other words, e-commerce site owners learn when the order is processed & delivered. However, this default version does not show all the order status transitions. You cannot target a transition ranging from “Processing” to “Custom Status”. Further, we will decode how to send a send email on custom order status. 

Implementing custom order status is quite complex. As in you cannot activate it directly through WooCommerce> Settings> Emails. In short, no email will trigger unless you code. So, how to enable this functionality?


PHP Snippet: Send Email on Custom Order Status in WooCommerce

// Targets custom order status "refused"
// Uses 'woocommerce_order_status_' hook
add_action( 'woocommerce_order_status_refused', 'phpsof_status_custom_notification', 20, 2 );
function phpsof_status_custom_notification( $order_id, $order ) {
    $heading = 'Order Refused';
    $subject = 'Order Refused';
    // Get WooCommerce email objects
    $mailer = WC()->mailer()->get_emails();
    // Use one of the active emails e.g. "Customer_Completed_Order"
    // Wont work if you choose an object that is not active
    // Assign heading & subject to chosen object
    $mailer['WC_Email_Customer_Completed_Order']->heading = $heading;
    $mailer['WC_Email_Customer_Completed_Order']->settings['heading'] = $heading;
    $mailer['WC_Email_Customer_Completed_Order']->subject = $subject;
    $mailer['WC_Email_Customer_Completed_Order']->settings['subject'] = $subject;
    // Send the email with custom heading & subject
    $mailer['WC_Email_Customer_Completed_Order']->trigger( $order_id );
    // To add email content use
   // You have to use the email ID chosen above and also that $order->get_status() == "refused"

Where do you add this snippet?

So, this is how you enable this functionality at admin panel. Doing so, you will be notified on the order progress. This is how you can serve your customers in a better way. To apply this code, just add this PHP snippet at your child theme function.php file’s bottom. It should be placed before “?>”, if you have it there. Apart from this, CSS goes in your child theme style.css file. 

Is this snippet still valid?

So, this is how you Send Email on Custom Order Status in WooCommerce. It is a great function that informs e-commerce store owner on every order status. This is how the owner can take quicker & better decisions for pending orders. So, I have applied this code on the Storefront theme and WordPress friendly hosting PHP 7.3. Let me know if everything works as expected. Share it further if you find this snippet useful for you and it had saved your time.

Also Read, How to Hide “Shipping Calculator” Fields @ Cart in WooCommerce?

How to Remove Checkout Billing Fields if Virtual Product @ Cart in WooCommerce

Important links: WooCommerce –