Avoid customer email notification for a specific product category in Woocommerce

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-25 07:24:06

问题


In Woocommerce, I am trying to stop customer order email for a particular product category in woocommerce.

What I have tried is:

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

But this hook is not working at all. Any help is appreciated.


回答1:


You should try this instead (with has_term() WP conditional function):

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

This code goes on function.php file of your active child theme (or theme). Tested and works.

This will avoid only Customer "processing" order email notification.

For Others customer order email notification, you will have to add also:

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

  • How to get WooCommerce order details
  • Get Order items and WC_Order_Item_Product in Woocommerce 3
  • Wordpress has_term() function (for custom taxonomy as a conditional tag)


来源:https://stackoverflow.com/questions/49168377/avoid-customer-email-notification-for-a-specific-product-category-in-woocommerce

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