每订单一个类别Woocomerce [英] One Category Per Order Woocomerce

查看:58
本文介绍了每订单一个类别Woocomerce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图限制Woocommerce商店上的客户一次只能订购1个类别.我正在尝试的代码出于某种原因只是限制了所有内容.

I'm trying to restrict customers on my Woocommerce store to only be allowed to order from 1 category at a time. The code I am trying is just restricting everything for some reason.

function is_product_the_same_cat($valid, $product_id, $quantity) {
global $woocommerce;
// start of the loop that fetches the cart items
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
    $terms = get_the_terms( $_product->id, 'product_cat' );
    $target_terms = get_the_terms( $product_id, 'product_cat' ); //get the current items
    foreach ($terms as $term) {
        $cat_ids[] = $term->term_id;  //get all the item categories in the cart
    }
    foreach ($target_terms as $term) {
        $target_cat_ids[] = $term->term_id; //get all the categories of the product
    }           
}
$same_cat = array_intersect($cat_ids, $target_cat_ids); //check if they have the same category
if(count($same_cat) > 0) return $valid;
else {
    wc_add_notice( 'This product is in another category!', 'error' );
    return false;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);

我并不想将其限制为每个类别1个产品,而是想限制它们,以便他们只能从每个订单订购1个类别的产品.一旦他们将糖果"类别的产品添加到购物篮,例如,他们将无法添加糖果"以外的其他任何类别的产品.

I'm not trying to limit it to 1 product per category, I'm trying to restrict them so they can only order products from 1 category per order. Once they've added a product to their basket from the category "Candy" for example, they then won't be able to add a product from any other category other than "Candy".

谢谢!

推荐答案

假定每个产品仅包含1个类别

Assuming that each product contains only 1 category

function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
    // If passed
    if ( $passed ) {
        
        // If cart is NOT empty when a product is added     
        if ( !WC()->cart->is_empty() ) {
            
            // Set vars
            $current_product_category_ids = array();
            $in_cart_product_category_ids = array();
            
            // Get current product categories via product_id
            $current_product_category_ids = wc_get_product_term_ids( $product_id, 'product_cat' );

            // Loop through cart items checking for product categories
            foreach ( WC()->cart->get_cart() as $cart_item ) {
                // Get product categories from product in cart via cart item product id
                $in_cart_product_category_ids = array_merge( $in_cart_product_category_ids, wc_get_product_term_ids( $cart_item['product_id'], 'product_cat' ) );
            }
            
            // Removes duplicate values
            $in_cart_product_category_ids = array_unique( $in_cart_product_category_ids, SORT_NUMERIC );
            
            // Compare
            $compare = array_diff( $current_product_category_ids, $in_cart_product_category_ids );
            
            // Result is NOT empty
            if ( !empty ( $compare ) ) {
                wc_add_notice( 'This product is in another category!', 'error' );
                $passed = false;
            }
        }
    }

    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );

这篇关于每订单一个类别Woocomerce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆