简体   繁体   English

Woocommerce 中定义的产品类别中只允许购物车中的一件商品

[英]Allow only one item in cart from defined product categories in Woocommerce

In Woocommerce, I found a bit of code that restricts a users purchase to one per category for category a or b.在 Woocommerce 中,我发现了一些代码,将用户购买限制为类别 a 或 b 的每个类别。 So currently the user could purchase 2 items 1 from cat a and 1 from cat b.所以目前用户可以从猫 a 购买 2 件商品 1,从猫 b 购买 1 件。 I would like to limit the user to only one product from category a or b.我想限制用户只能使用 a 类或 b 类中的一种产品。 The code I am working with is below, thanks in advance.我正在使用的代码如下,提前致谢。

add_filter( 'woocommerce_add_to_cart_validation', 'allowed_quantity_per_category_in_the_cart', 10, 2 );function allowed_quantity_per_category_in_the_cart( $passed, $product_id) {

$max_num_products = 1;// change the maximum allowed in the cart
$running_qty = 0;

$restricted_product_cats = array();

//Restrict particular category/categories by category slug
$restricted_product_cats[] = 'cat-a, cat-b';


// Getting the current product category slugs in an array
$product_cats_object = get_the_terms( $product_id, 'product_cat' );
foreach($product_cats_object as $obj_prod_cat) $current_product_cats[]=$obj_prod_cat->slug;


// Iterating through each cart item
foreach (WC()->cart->get_cart() as $cart_item_key=>$cart_item ){

    // Restrict $max_num_products from each category
    if( has_term( $current_product_cats, 'product_cat', $cart_item['product_id'] )) {

    // Restrict $max_num_products from restricted product categories
    //if( array_intersect($restricted_product_cats, $current_product_cats) && has_term( $restricted_product_cats, 'product_cat', $cart_item['product_id'] )) {

        // count(selected category) quantity
        $running_qty += (int) $cart_item['quantity'];

        // More than allowed products in the cart is not allowed
        if( $running_qty >= $max_num_products ) {
            wc_add_notice( sprintf( 'Only %s '.($max_num_products>1?'products from this category are':'product from this category is').' allowed in the cart.',  $max_num_products ), 'error' );
            $passed = false; // don't add the new product to the cart
            // We stop the loop
            break;
        }

    }
}
return $passed;}

Try this (but it doesn't handle quantities as is not clear and much more complicated, because in cart page they can be altered) :试试这个(但它不处理数量,因为不清楚而且更复杂,因为在购物车页面中它们可以被更改)

add_filter( 'woocommerce_add_to_cart_validation', 'limit_cart_items_from_category', 10, 3 );
function limit_cart_items_from_category ( $passed, $product_id, $quantity )
{
    // Accept when cart is empty
    if( WC()->cart->is_empty() ) return $passed;

    // HERE your product categories in this array (can be names, slugs or Ids)
    $categories = array('T-shirts', 'Hoodies');
    $found = $current = false;

    // Check the current product
    if( has_term( $categories, 'product_cat', $product_id ) ){
        $current = true;
    }

    // Loop through cart items checking for product categories
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $found = true;
            break; // stop the loop.
        }
    }

    // Current product and a cart item match with defined product categories
    if( $found && $current ){
        $passed = false;
        $cats_str = implode('" and "', $categories );
        wc_add_notice( sprintf( __('Only one item is allowed from "%s" categories…', 'woocommerce' ), $cats_str ), 'error' );
    }
    return $passed;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and work.测试和工作。

Thank You For your Code I modified this code and make my customized code.谢谢你的代码我修改了这个代码并制作了我的自定义代码。 when customer add to cart the product from defined category, after that they didnt allow to cart other category product.当客户将定义类别的产品添加到购物车时,之后他们不允许购物其他类别的产品。 you can set the maximum number of product easily by just changing the number.您只需更改数量即可轻松设置产品的最大数量。 Thank You once again.再一次谢谢你。

add_filter( 'woocommerce_add_to_cart_validation', 'limit_cart_items_from_category', 10, 3 );
function limit_cart_items_from_category ( $passed, $product_id, $quantity )
{
    // change the number for maximum product allowed in the cart
    $max_num_products = 5; // Here change the number for maximum allowed product in the cart
    // Accept when cart is empty
    if( WC()->cart->is_empty() ) return $passed;
    

    // HERE your product categories in this array (can be names, slugs or Ids)
    $categories = array('Sea-Food-Chaasum');
    $found = $current = false;

    // Check the current product
    if( has_term( $categories, 'product_cat', $product_id ) ){
        $current = true;
    }
    
    

    // Loop through cart items checking for product categories
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $found = true;
            $current = true;
            break; // stop the loop.
        }
    }
    
    
    // count(selected category) quantity
        $running_qty += (int) $cart_item['quantity'];

        // More than allowed products in the cart is not allowed
        if( $running_qty >= $max_num_products )
            

    // Current product and a cart item match with defined product categories
    if( $found && $current ){
        $passed = false;
        $cats_str = implode('" and "', $categories );
        wc_add_notice( sprintf( __('Only one item is allowed from "%s" categories…', 'woocommerce' ), $cats_str ), 'error' );
    }
    return $passed;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Woocommerce中仅允许更新一个商品类别的购物车商品数量 - Allow cart item quantity update for only one product category in Woocommerce 禁用 Woocommerce 中特定类别的购物车项目的其他产品类别 - Disable other product categories for a cart item from specific category in Woocommerce 基于 WooCommerce 产品类别购物车项目计数的费用 - Fees based from WooCommerce product categories cart item count Woocommerce:如何在购物车中只允许一种产品类型? - Woocommerce: How to allow only one product type in a cart? 在 Woocommerce 中一次只允许购物车中的一个产品类别 - Allow only one product category in cart at once in Woocommerce Woocommerce - 从购物车中仅删除一件产品 - Woocommerce - Remove Only one Unit of a product from cart 更改Woocommerce中特定产品类别的购物车项目价格 - Change cart item prices for specific product categories in Woocommerce 显示特定产品类别的 WooCommerce 购物车项目简短描述 - Display WooCommerce cart item short description for a specific product categories WooCommerce 中特定产品类别的最小购物车商品数量 - Minimum cart item quantity for specific product categories in WooCommerce Woocommerce购物车中仅允许两种产品中的一种 - Allow only one from two products in Woocommerce cart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM