【问题标题】:Avoid customer email notification for a specific product category in Woocommerce在 Woocommerce 中避免针对特定产品类别的客户电子邮件通知
【发布时间】:2018-08-16 12:38:51
【问题描述】:

在 Woocommerce 中,我正在尝试停止针对 woocommerce 中特定产品类别的客户订单电子邮件。

我试过的是:

add_filter('woocommerce_email_recipient_customer_processing_order', 'wc_change_customer_new_order_email_recipient', 10, 3);
function wc_change_customer_new_order_email_recipient( $recipient, $order ) {
 global $woocommerce;
    $items = $order->get_items();
    $category_ids = array( 10 );
    $flagHasProduct = false;
    foreach ( $items as $item ) {
        $product_id = $item['product_id'];
        $terms = get_the_terms( $product_id, 'product_cat' ); 

        foreach ( $terms as $term ) {  
            if( in_array( $term->term_id, $category_ids ) ) {
                $flagHasProduct = true;
            }
        }

    }
    if ($flagHasProduct) {
        $recipient = "";
    } 
    $recipient = "";
    return $recipient;
}

但是这个钩子根本不起作用。任何帮助表示赞赏。

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    你应该试试这个(使用has_term() WP 条件函数)

    add_filter( 'woocommerce_email_recipient_customer_processing_order', 'product_cat_avoid_processing_email_notification', 10, 2 );
    function product_cat_avoid_processing_email_notification( $recipient, $order ) {
        if( is_admin() ) return $recipient;
    
        // HERE set your product categories (coma separated term Ids, slugs or names)
        $product_categories = array( 10 );
    
        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            // Get an instance of the WC_Product object
            $product = $item->get_product(); 
            // Get the correct product ID (for variations we take the parent product ID)
            $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
    
            // Check for product categories for this item
            if( has_term( $product_categories, 'product_cat', $product_id ) )
                return ''; // If it's found, we return an empty recipient
        }
        return $recipient;
    }
    

    此代码位于您的活动子主题(或主题)的 function.php 文件中。经过测试并且有效。

    这将避免仅客户“处理”订单电子邮件通知

    对于其他客户订单电子邮件通知,您还必须添加:

    // Customer "completed" Order email notification
    add_filter( 'woocommerce_email_recipient_customer_completed_order', 'product_cat_avoid_processing_email_notification', 10, 2 );
    
    // Customer "on-hold" Order email notification
    add_filter( 'woocommerce_email_recipient_customer_on_hold_order', 'product_cat_avoid_processing_email_notification', 10, 2 );
    
    // Customer "refunded" Order email notification
    add_filter( 'woocommerce_email_recipient_customer_refunded_order', 'product_cat_avoid_processing_email_notification', 10, 2 );
    
    // Customer "invoice" email notification
    add_filter( 'woocommerce_email_recipient_customer_invoice', 'product_cat_avoid_processing_email_notification', 10, 2 );
    

    【讨论】:

      猜你喜欢
      • 2021-04-25
      • 1970-01-01
      • 2018-04-10
      • 2020-10-03
      • 2019-10-07
      • 2021-04-02
      • 2019-07-19
      • 2021-03-22
      • 2019-04-09
      相关资源
      最近更新 更多