2
votes

Normal behavior of Woocommerce is:

  • If order is "pending" (Awaiting payment) it does not send a "New Order" email
  • If order is "on Hold"(Awaiting confirmation) it sends "New Order" email.
  • If order is immediately set from "Pending" to "Processing" (by payment gateway) - it sends "New Order" email.

I want to send "New Order" email only when order status reaches "Processing" (no mater the previous status)

So Ultimately the objective is to stop woocommerce from sending "New Order" email on order status "hold" and to make it send only (and always) when status gets to "Processing".

First I have tried WooCommerce send email notification to admin for specific order status answer code that works perfectly for successfully triggering the "New Order" email on order status "processing", however the email still gets send on order status "hold" (so sometimes it might be sent twice).

Trying to go around this by disabling the "New Order" email on "Hold" status by going to:

Admin >> Woocommerce >> Settings >> Emails

then disabling "New Order" order email by clicking manage button, does not work (this disabled the email entirely and it stops sending entirely even when triggered on processing by the above code)

Then I tried Disable WooCommerce New order email notification if order status is On hold answer code.

However it disables successfully the New Order email, but if the order goes from "On Hold" to "Processing" you don't get any email at all and if a new order is set directly to processing and you have the previous code for triggering the email then you also have the problem of receiving 2 emails. Switching the status from:

return $order->get_status() === 'on-hold' ? '' : $recipient;

to:

return $order->get_status() === 'processing' ? '' : $recipient;

disables the "New order" email entirely.

I basically wanted to document my struggle with this and share my current solution because there seems to be quite a few people with this same intention but no clear concise and actual functional way of doing it.

1
Important: See this thread: stackoverflow.com/questions/66604256/…LoicTheAztec

1 Answers

2
votes

This is what I have managed to make work properly, removing all New Order triggers possibilities entirely (as provided here by woocommerce):

/**
 * Unhook and remove WooCommerce all default "New Order" emails.
 */

add_action( 'woocommerce_email', 'unhook_those_pesky_emails' );

function unhook_those_pesky_emails( $email_class ) {
    // New order emails
    remove_action( 'woocommerce_order_status_on-hold_to_processing_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
    remove_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
    remove_action( 'woocommerce_order_status_pending_to_processing_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
}

And using the following trigger (provided by @LoicTheAztec in this thread)

/** 
 * trigger "New Order" email on "processing" status
 */

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

It's probably not clean and very likely not the most optimized way, but it is the only way I have found to successfully make "New Order" emails only be sent when orders have been paid (set to processing status) and hope it manages to help someone else.

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