Search code examples
phpwordpresswoocommerceordersemail-notifications

WooCommerce send email notification to admin for specific order status


Here below is my code for sending notification email to admin when order status becomes "processing":

add_action( 'woocommerce_checkout_order_processed', 'process_new_order_notification', 20, 1 );

function process_new_order_notification( $order_id ) {

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    //Order status processing
    if( ! $order->has_status( 'processing' ) ) return;

    // Send "New Email" notification (to admin)
    WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}

But it doesn't work as the admin don't receive any email when order status becomes processing. I think there something wrong with my code. Any help?


Solution

  • You can use woocommerce_order_status_$STATUS_TRANSITION[to] composite hook for "processing"status transition changing $STATUS_TRANSITION[to] to processing, which will simplify and compact the code, like:

    add_action( 'woocommerce_order_status_processing', 'process_new_order_notification', 20, 2 );
    function process_new_order_notification( $order_id, $order ) {
        // Send "New Email" notification (to admin)
        WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
    }
    

    Code goes in function.php file of your active child theme (or active theme). It should better work.

    Since WooCommerce 5+: Allow re-sending New Order Notification in WooCommerce 5+