Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @always1learning

    Thanks for reaching out!

    Is there a code snippet I can add to trigger the email to be sent, anytime an order is changed to “cancelled”?

    I found the code snippet below from this article that worked on my site:

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

    Output:

    Hope this helps!

    Thread Starter always1learning

    (@always1learning)

    Thank you!

    One follow up question, where can I find a list of all the possible “order statuses” and “WC_email objects”.

    I’ve looked through the Woo Commerce code reference and documentation, but have not found it yet. Thanks so much.

    Hi @always1learning

    You are most welcome and I’m glad that worked! ??

    One follow up question, where can I find a list of all the possible “order statuses” and “WC_email objects”.

    I’ve looked through the Woo Commerce code reference and documentation, but have not found it yet. Thanks so much.

    You can find the list of all built-in Order Status in WooCommerce in this support documentation: https://woocommerce.com/document/managing-orders/#order-statuses

    Hope this helps!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Code snippet to send admin an email on ALL Cancelled Orders’ is closed to new replies.