Sending email to customer on cancelled order in Woocommerce

前端 未结 1 2023
孤街浪徒
孤街浪徒 2020-12-09 00:09

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 th

相关标签:
1条回答
  • 2020-12-09 01:10

    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

    0 讨论(0)
提交回复
热议问题