0

I have added a custom shipping option which works great, however duplicate emails are sent - my custom shipping new order email and the standard admin new order email.

In WooCommerce > Settings > Email I have added a new option for my custom shipping and added recipients and so on. What I think I need to do now is put a condition in to the Admin New Order section, something along the lines of..

if(shipping method == 'Custom Shipping')
      return; // this can just bail out as a custom shipping email is being sent

I think this will stop the standard new order email being sent if the order uses Custom Shipping.

Can anyone give me guidance on how I can implement this? Do I put code in to the admin-new-order.php file with this if statement? Thank you

Zeus7
  • 128
  • 1
  • 7

1 Answers1

1

You could remove the standard mail actions: https://docs.woocommerce.com/document/unhookremove-woocommerce-emails/

And then intercept the order_status_changed hook to trigger your mail depending on conditions (like the shipping method): https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html

function your_function($id)
{

    $order = wc_get_order($id);

    if (!empty($order) && $order->get_shipping_method() === 'Custom Shipping') {
        do_action('woocommerce_before_resend_order_emails', $order, 'email_name');
        // prepare your email
        do_action('woocommerce_after_resend_order_email', $order, 'email_name');
    }
}
add_action('woocommerce_order_status_changed', 'your_function');
Johannes
  • 1,478
  • 1
  • 12
  • 28
  • Hi Johannes, thank you very much. Sorry I have edited this comment as I missed the before and after resends. For the 'email_name' field, is this what the title is in WooCommerce > Settings > Email? – Zeus7 Jun 10 '20 at 14:25
  • Yes, it's the `id` of the email you want to send, for example `customer_processing_order` (see GitHub repository of WooCommerce: https://github.com/woocommerce/woocommerce/tree/452e53d8afa4c65cbace926ff63b582193eea03c/includes/emails and check the `id` assignment in each email) – Johannes Jun 10 '20 at 14:56
  • @Zeus7 Please mark this answer as `accepted` if it worked for you to help others to find a proper solution faster. – Johannes Jun 12 '20 at 08:22