8

I am trying to send an email to the client when an order gets cancelled. By default, woocommerce only sends this email only to the admin of the site. This code has solved the issue for related posts on the web:

function wc_cancelled_order_add_customer_email( $recipient, $order ){
   return $recipient . ',' . $order->billing_email;
}
add_filter( 'woocommerce_email_recipient_cancelled_order', 'wc_cancelled_order_add_customer_email', 10, 2 );
add_filter( 'woocommerce_email_recipient_failed_order', 'wc_cancelled_order_add_customer_email', 10, 2 );

However, it seems like woocommerce removed those filter hooks completely. Is there any way of doing this?

Thanks in advance!

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
KevDev
  • 150
  • 1
  • 1
  • 8

1 Answers1

36

In this custom function hooked in woocommerce_order_status_changed action hook, I am targeting "cancelled" and "failed" orders sending an the corresponding email notification to the customer (as admin will receive it on his side by WooCommerce automated notifications):

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

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This should works in WooCommerce 3+

If you need, instead of changing the email, you can add it, to existing recipients:

// Add a recipient in this instance
$wc_emails['WC_Email_Failed_Order']->recipient .= ',' . $customer_email;

Related answer: Send an email notification when order status change from pending to cancelled

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 4
    It worked, thanks! You really saved my day with that, i've been sitting way too long on this problem. Have a beautiful day sir! – KevDev Dec 05 '17 at 09:23
  • Nice solution, is there a way to change the content text of the email? I would like the admin notification text to be different to the customer one. Would gettext be the answer? – fightstarr20 Apr 25 '18 at 15:17
  • bless this action, and for those who want to custom email i think they should see here : https://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/. Just disable the trigger in that plugins. and you can use this function to trigger $wc_emails['WC_Expedited_Order_Email']->trigger( $order_id ); – Anthony Kal Nov 07 '18 at 07:01
  • you can also custom the email template in woocommerce email. – Anthony Kal Nov 07 '18 at 07:08
  • Great. Still working also in WooCommerce 6.4.1 – Marco May 11 '22 at 16:34
  • For me, this code creates a problem when cancelling orders in bulk. If you have two orders, A and B, their collective cancellation results in person A and admin getting an email about cancelling order A, and person B getting an email about cancelling orders A and B. So person B gets information about an order that isn't theirs at all. – omicito Jan 24 '23 at 10:57