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

时间:2017-04-08 08:52:36

标签: notifications woocommerce hook-woocommerce

我可以参考此功能来禁用电子邮件通知: https://docs.woocommerce.com/document/unhookremove-woocommerce-emails/

但我想仅为特定产品禁用它,或者如果它更简单,则禁用特定产品类别。

感谢您的帮助

3 个答案:

答案 0 :(得分:3)

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

您可以尝试一件事 - 使用收件人的钩子,您可以删除收件人电子邮件并返回空字符串。或者如果空字符串触发错误,那么您可以给出一些虚拟电子邮件。

使用此代码:

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

答案 1 :(得分:3)

感谢@ 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 );

答案 2 :(得分:1)

以上代码将禁用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中可以正常工作。

相关问题