Home » WooCommerce: Send a Custom Email on Order Status Change

WooCommerce: Send a Custom Email on Order Status Change

by Tutor Aspire

If you use custom WooCommerce order statuses, or wish to target an order status transition that is not default (e.g. when order goes from “Processing” to “Completed”, the Completed email triggers – but what if you want to target a transition from “Processing” to “Custom Status”?), sending custom emails is quite complex.

First of all, they won’t show under WooCommerce > Settings > Emails (unless you code it, true) – second, no email will trigger. So, how do they do it?

Send a custom email when an order is marked with custom status – WooCommerce

Snippet (PHP): Send WooCommerce Email @ Custom Order Status

/**
 * @snippet       Send Formatted Email @ WooCommerce Custom Order Status
 * @how-to        Get tutoraspire.com FREE
 * @sourcecode    https://tutoraspire.com/?p=91907
 * @author        Tutor Aspire
 * @compatible    WooCommerce 3.5.7
 * @donate $9     https://tutoraspire.com
 */
 
// Targets custom order status "refused"
// Uses 'woocommerce_order_status_' hook
 
add_action( 'woocommerce_order_status_refused', 'tutoraspire_status_custom_notification', 20, 2 );
 
function tutoraspire_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 https://businessbloomer.com/woocommerce-add-extra-content-order-email/
    // You have to use the email ID chosen above and also that $order->get_status() == "refused"
     
}

You may also like