Send Email Notification to Customers For Order Status Change in WooCommerce

Send email notification to customers if the order status for a customers order is changed from failed or cancelled in WooCommerce on child sites.

Snippet Type

Execute on Child Sites

Snippet

add_action('woocommerce_order_status_changed', 'send_custom_email_notifications', 10, 4 );
function send_custom_email_notifications( $order_id, $old_status, $new_status, $order ){
    if ( $new_status == 'cancelled' || $new_status == 'failed' ){
        $wc_emails = WC()->mailer()->get_emails(); // Get all WC_emails objects instances
        $customer_email = $order->get_billing_email(); // The customer email
    }

    if ( $new_status == 'cancelled' ) {
        // change the recipient of this instance
        $wc_emails['WC_Email_Cancelled_Order']->recipient = $customer_email;
        // Sending the email from this instance
        $wc_emails['WC_Email_Cancelled_Order']->trigger( $order_id );
    } 
    elseif ( $new_status == 'failed' ) {
        // change the recipient of this instance
        $wc_emails['WC_Email_Failed_Order']->recipient = $customer_email;
        // Sending the email from this instance
        $wc_emails['WC_Email_Failed_Order']->trigger( $order_id );
    } 
}
4 Likes

Hi,

This code works well with single-order status change.
But if you use bulk order change it sends all selected mails to all customers not just their order.
The code fetches all customer mails and puts them to TO in the mail that is the problem cause all others get emails with the customer’s personal data!

Should be able to exclude by bulk edit on orders in WooCommerce.