1

I can refer to this function to disable email notification: https://docs.woocommerce.com/document/unhookremove-woocommerce-emails/

But I would like to disable it only for a specific product or, if it can be more simple, for a specific product category.

Thanks for your help

jjj
  • 965
  • 8
  • 23

3 Answers3

6

Thanks to @vidish-purohit for the help!

Here is my code to use if you need to disable admin email notification for a specific product:

function change_email_recipient_depending_of_product_id( $recipient, $order ) {
    global $woocommerce;
    $items = $order->get_items();
    foreach ( $items as $item ) {
        $product_id = $item['product_id'];
        if ( $product_id == xxx ) {
            $recipient = '';
        }
        return $recipient;
    }
}
add_filter( 'woocommerce_email_recipient_new_order', 'change_email_recipient_depending_of_product_id', 10, 2 );

And if you need to disable customer email notification for a specific product:

function change_email_recipient_depending_of_product_id( $recipient, $order ) {
    global $woocommerce;
    $items = $order->get_items();
    foreach ( $items as $item ) {
        $product_id = $item['product_id'];
        if ( $product_id == xxx ) {
            $recipient = '';
        }
        return $recipient;
    }
}
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'change_email_recipient_depending_of_product_id', 10, 2 );
jjj
  • 965
  • 8
  • 23
4

I think when you try to hook email notification from template, where you can find order, at that time emails are already sent.

You can try one thing - using recipient's hook you can remove recipient email and return empty string. Or if empty string triggers error, then you can give some dummy email.

Use this code for this:

// Change new order email recipient for registered customers
function wc_change_admin_new_order_email_recipient( $recipient, $order ) {
    global $woocommerce;

    // check if product in order
    if ( true ) ) {
        $recipient = "";
    } else {
        $recipient = "newbusiness@yourdomain.com";
    }
    return $recipient;
}
add_filter('woocommerce_email_recipient_new_order', 'wc_change_admin_new_order_email_recipient', 1, 2);

// Change new order email recipient for registered customers
function wc_change_admin_new_order_email_recipient( $recipient, $order ) {

    $flagHasProduct = false;

    // Get items in order
    $items = $order->get_items(); 

    // Loop for all items
    foreach ( $items as $item ) {
       $product_id = $item['product_id'];

        // check if specific product is in order
        if ( $product_id == 102 ) {
            $flagHasProduct = true;
        }
    }

    // if product is found then remove recipient
    if ($flagHasProduct) {
        $recipient = "";
    } else {
        $recipient = "newbusiness@yourdomain.com";
    }
    return $recipient;
}
add_filter('woocommerce_email_recipient_new_order', 'wc_change_admin_new_order_email_recipient', 1, 2);
Vidish Purohit
  • 1,048
  • 11
  • 20
  • Thanks for your help! yes it can be a solution to remove recipient email, interesting... now the difficult part is to write the condition (if product id is...). – jjj Apr 08 '17 at 12:41
  • 2
    If you see the function arguments, you are getting order object as second argument. You can get all items in order from that and check if specific product is in the order. Let me know if you still need assistance. – Vidish Purohit Apr 08 '17 at 13:30
  • Thank you Vidish, yes assistance would be appreciate, if we assume that the product ID is 102, how the code will look like? – jjj Apr 08 '17 at 15:59
  • 2
    I have edited my answer and added complete solution. You can use this code. – Vidish Purohit Apr 08 '17 at 17:46
  • If you find my answer useful, then you can accept it and upvote it! – Vidish Purohit Apr 08 '17 at 18:04
  • Thank you Vidish! I forgot to mention that I would like to disable email notification for customers, not for the admin so I replaced `woocommerce_email_recipient_new_order` by `woocommerce_email_recipient_customer_processing_order` but it is not working, maybe I miss something? – jjj Apr 10 '17 at 06:06
  • You can try this hook! "woocommerce_email_recipient_processing_order". And if my answer helped you, then you can accept it as answer and upvote it! – Vidish Purohit Apr 10 '17 at 06:17
  • @VidishPurohit The correct hook is not ```woocommerce_email_recipient_processing_order```, but ```woocommerce_email_recipient_customer_processing_order``` – Caín Sep 30 '22 at 18:25
1

The above code will disable the email option in the Woocommerce email setting option page.

/**
 * Disable Admin email Notification for Specific Product
 */

function cstm_change_email_recipient_for_giftcard_product($recipient, $order)
{
    // Bail on WC settings pages since the order object isn't yet set yet
    // Not sure why this is even a thing, but shikata ga nai
    $page = $_GET['page'] = isset($_GET['page']) ? $_GET['page'] : '';
    if ('wc-settings' === $page) {
        return $recipient;
    }

    // just in case
    if (!$order instanceof WC_Order) {
        return $recipient;
    }

    $items = $order->get_items();
    foreach ($items as $item) {
        $product_id = $item['product_id'];
        if ($product_id == xxxx) {
            $recipient = '';
        }
        return $recipient;
    }
}

add_filter('woocommerce_email_recipient_new_order', 'cstm_change_email_recipient_for_giftcard_product', 10, 2);

this code works fine in latest version of Woocommerce.

Rigal
  • 611
  • 2
  • 6
  • 25