Search code examples
wordpresswoocommercehook-woocommerceemail-notificationsshipping-method

Disable WooCommerce new order email notification when shipping method is NOT "Local Pickup"


I am trying to send the Woocommerce New Order email ONLY when the customers has selected "Local Pickup" as shipping method.

In order to achieve that i disabled the new order email notification in the WooCommerce settings while trying to enabled the new order email with a filter if the condition shipping method = local pickup is met.

So far i was unable to pull it off. Here is my filter:

add_filter( 'woocommerce_email_enabled_new_order', 'lds_only_send_mail_when_shipping_is_pickup', 10, 2 );

function lds_only_send_mail_when_shipping_is_pickup($order) {

    if (!empty($order) && $order->get_shipping_method() === 'local_pickup') {
      
      return true;
    
    } 
    
}

Not sure what I am doing wrong?

As the first parameter of woocommerce_email_enabled_{id} is optional, i thought it would be enough to just return true when the condition is met.


Solution

  • No need to make any changes to WooCommerce settings

    To disable WooCommerce new order email notification if order shipping method is NOT equal to "Local Pickup", you can use the woocommerce_email_recipient_new_order hook

    So you get:

    function filter_woocommerce_email_recipient_new_order( $recipient, $order = false ) {   
        if ( ! $order || ! is_a( $order, 'WC_Order' ) ) return $recipient;
        
        // Get shipping method
        $shipping_method = $order->get_shipping_method();
    
        // NOT equal (Note: this should be adjusted to the shipping method in your site language)
        // Such as: 'Afhalen' for dutch, etc...
        if ( $shipping_method != 'Local Pickup' ) {
            $recipient = '';
        }
    
        return $recipient;
    }
    add_filter( 'woocommerce_email_recipient_new_order', 'filter_woocommerce_email_recipient_new_order', 10, 2 );