Disable WooCommerce email notification for specific product

ⅰ亾dé卋堺 提交于 2019-12-22 00:43:06

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/43292048/disable-woocommerce-email-notification-for-specific-product

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!