0

WooCommerce default mode is to send an email to the client when order status change happens. But how can the admin / store manager know when these changes are made? Keeping the orders page open 24/7 and refresh the view is not a solution.

I have tried the following code, however this does not work for custom order status.

add_filter('woocommerce_email_recipient_customer_processing_order', 'email_recipient_custom_notification', 10, 2);
function email_recipient_custom_notification( $recipient, $order ) {
                    
      if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
                    
      error_log("Hello: woocommerce_email_recipient_customer_processing_order : recipient = " . $recipient . "\n" );
                    
      // Set HERE your replacement recipient email(s)… (If multiple, separate them by a coma)
      $recipient .= ', admin@xyz.com';
      return $recipient;
 }
                
add_filter('woocommerce_email_recipient_customer_out-to-delivery_order', 'email_out_to_delivery_notification', 10, 2);
function email_out_to_delivery_notification( $recipient, $order ) {
                    
      if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
                    
      error_log("Hello: woocommerce_email_recipient_customer_out-to-delivery_order : recipient
= " . $recipient . "\n" );
                    
      // Set HERE your replacement recipient email(s)… (If multiple, separate them by a coma)
      $recipient .= ', admin@xyz.com';
      return $recipient;
}

Note that 'out-to-delivery' is the custom order status that I have successfully added. I am able to change the order status from "On-hold --> Processing --> Out-To-Delivery --> Completed".

With the above changes site admin get the notification for 'New-Order' and 'Processing' order status. But he doesn't get notification for 'Out-To-Delivery' or 'Completed' status change. Seems that the filter 'woocommerce_email_recipient_customer_out-to-delivery_order' that I have used doesn't work.

Thanks

user2679476
  • 365
  • 3
  • 12
  • You can try with this hook `woocommerce_process_shop_order_meta` – Mainul Hasan Oct 28 '21 at 05:46
  • 2
    The hook you are using adds recipients for existing email notifications that will be sent anyway. No emails will be sent for custom order statuses, unless you have effectively provided this. Is this the case? _"Note that 'out-to-delivery' is the custom order status that I have successfully added."_ - Can you give more details on how you added this/these so that your question contains [a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – 7uc1f3r Oct 28 '21 at 06:47
  • Hi @MainulHasan , the hook 'woocommerce_process_shop_order_meta' is invoked when order status changed. But my objective is to send email notification to shop admin. How to do that using this hook ? – user2679476 Oct 28 '21 at 17:37
  • @user2679476 The following answer contains 90% of the answer to your question -> [Add a new order status that Send an email notification in WooCommerce 4+](https://stackoverflow.com/a/64608630/11987538) - The only thing you need to add is that the emails will also be sent to the admin, and that is already applicable in your code attempt – 7uc1f3r Oct 29 '21 at 07:57
  • Hi @7uc1f3r, In fact I tried this link. But it gives error (There has been a critical error on this website. Please check your site admin email inbox for instructions.) when I add this line :- add_action( 'woocommerce_order_status_wc-out-to-delivery', array( WC(), 'send_transactional_email' ), 10, 1 ); – user2679476 Oct 30 '21 at 11:57
  • In continuation of the above comment : The exception is as follows : An error of type E_ERROR was caused in line 598 of the file .php..... Error message: Uncaught Error: Call to undefined function WC() in /public_html/wp-content/plugins/myplugin/.php:598 – user2679476 Oct 30 '21 at 12:12
  • @user2679476 ok, what do you expect from the mail sent for your custom status? There are different types of emails. **1)** complete blank mail where you determine the content yourself (hard coded). **2)** an email based on the existing WooCommerce emails, where you will just overwrite some text and titles (hard coded). **3)** An email created via a custom template file? (hard coded, but customizable via the WooCommerce settings) – 7uc1f3r Oct 31 '21 at 08:27
  • Hi @7uc1f3r, I am using existing WooCommerce email template. I just want the email notification to be sent to site admin and customer - both when the status is changed to "Out-To-Delivery". – user2679476 Nov 01 '21 at 05:05

1 Answers1

0

The following code works for me for sending email notification on custom order-status change:

add_filter('woocommerce_email_recipient_customer_processing_order', 'email_recipient_custom_notification', 10, 2);
function email_recipient_custom_notification( $recipient, $order ) {
    
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
    
    // Set HERE your replacement recipient email(s)… (If multiple, separate them by a coma)
    $recipient .= ', admin@xyz.com';
    return $recipient;
}

add_action('woocommerce_order_status_changed', 'send_custom_email_notifications', 10, 4 );

function send_custom_email_notifications( $order_id, $old_status, $new_status, $order ){
    
    $wc_emails = WC()->mailer()->get_emails(); // Get all WC_emails objects instances
    
    if ($new_status === 'out-to-delivery') {

        $wc_emails['WC_Email_Customer_Processing_Order']->trigger( $order_id );
    }
   
}
user2679476
  • 365
  • 3
  • 12