1
votes

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

3

3 Answers

4
votes

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 = "[email protected]";
    }
    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 = "[email protected]";
    }
    return $recipient;
}
add_filter('woocommerce_email_recipient_new_order', 'wc_change_admin_new_order_email_recipient', 1, 2);
5
votes

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 );
1
votes

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.