0

I implemented this code by @LoicTheAztec to set the default status for all COD orders to pending:

add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_change_order_status', 10, 1 );
function woocommerce_thankyou_change_order_status( $order_id ){
    if( ! $order_id ) return;
    $order = wc_get_order( $order_id );
    if( $order->get_status() == 'processing' ) $order->update_status( 'pending' );
} 

After adding the code (as a plugin), Woocommerce started sending duplicate email notifications of new orders. Essentially, one notification is sent when the order is created (rightly so), but the same exact notification is again then sent when the order is marked as completed.

How can I stop this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Ryan
  • 11
  • 6

1 Answers1

0

You are generating another notification since you are updating the order status after it has already been set by the default functionality in woocommerce. Can you try the below code:

add_filter( 'woocommerce_cod_process_payment_order_status', 'default_cod_payment_order_status', 10 );
function default_cod_payment_order_status( $order_status ) {
    return 'pending';
}
Omar Tanti
  • 1,368
  • 1
  • 14
  • 29
  • Thanks Omar. I tried this code, but it did not work. It didn't even change the status to 'pending' at all. – Ryan Aug 09 '19 at 22:15
  • Ooooh wait... I'm looking at your code and you're returning "processing", so maybe that's the reason why it didn't work. I'll edit it now and check. Brb... – Ryan Aug 09 '19 at 22:17
  • Okay, yeah it didn't work per se. It did change the status to 'pending' once I changed it in the code, but it did not generate ANY email at all. Not sure if this is the same code, but I've tried a code previously that did the same thing - changed the status but did not generate any notification email. – Ryan Aug 09 '19 at 22:23
  • I don't think that this filter should stop any emails from triggering. Please make sure that email settings are correct in woocommerce setting and also try commenting out other code you have done related to woccommerce to make sure you do not have any other code which is stopping such triggers – Omar Tanti Aug 11 '19 at 05:53
  • yes, the email settings in woocommerce are correct (they work fine otherwise) and I added the code you gave as a plugin so all I do is just activate and deactivate it. Curiously, when I apply your code, emails are not sent at all. I'm wondering it has to do with the stage at which the status change happens? – Ryan Aug 11 '19 at 22:03