禁用特定产品的 WooCommerce 电子邮件通知

Disable WooCommerce email notification for specific product

我可以参考这个函数来关闭邮件通知: https://docs.woocommerce.com/document/unhookremove-woocommerce-emails/

但我只想针对特定产品禁用它,或者,如果可以更简单的话,针对特定产品类别禁用它。

感谢您的帮助

我认为当您尝试从模板挂钩电子邮件通知时,您可以在其中找到订单,那时电子邮件已经发送。

您可以尝试一件事 - 使用收件人的挂钩您可以删除收件人电子邮件和 return 空字符串。或者,如果空字符串触发错误,那么您可以提供一些虚拟电子邮件。

为此使用此代码:

// 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 的帮助!

如果您需要禁用特定产品的 admin 电子邮件通知,请使用我的代码:

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

如果您需要禁用特定产品的客户电子邮件通知:

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

以上代码将禁用 Woocommerce 电子邮件设置选项页面中的电子邮件选项。

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

此代码在最新版本的 Woocommerce 中运行良好。