简体   繁体   English

根据Woocommerce中的产品类别将最大商品数量添加到购物车

[英]Max item quantity added to cart based on product category in Woocommerce

I am trying to customize the shop such that Category named Quantity4 allows only 4 items to be added in the cart and category named Quantity6 allows only items to be added in the cart. 我正在尝试自定义商店,以便名为Quantity4的类别仅允许在购物车中添加4个商品,名为Quantity6的类别仅允许在购物车中添加商品。

As far as I can get, this can be achieved using nested if statements, but somehow this doesn't work. 据我所知,这可以使用嵌套的if语句实现,但不知怎的,这不起作用。

add_filter( 'woocommerce_add_to_cart_validation', 'only_four_items_allowed_add_to_cart', 10, 3 );

function only_four_items_allowed_add_to_cart( $passed, $product_id, $quantity ) {

$cart_items_count = WC()->cart->get_cart_contents_count();
$total_count = $cart_items_count + $quantity;

if(has_term('quantity4','product_cat',$product_id )){
if($cart_items_count >= 4 || $total_count > 4){
        // Set to false
        $passed = false;
        // Display a message
        wc_add_notice( __( "You Chose a Box of 4 Items, Can't Add More", "woocommerce" ), "error" );
    }
  }

  else if (has_term('quantity6','product_cat',$product_id )){
    if($cart_items_count >= 6 || $total_count > 6){
        // Set to false
        $passed = false;
        // Display a message
        wc_add_notice( __( "You Chose a Box of 6 Items, Can't Add More", "woocommerce" ), "error" );
    }
}

return $passed;
}

Can someone pls point out what am I doing wrong here and how to get the desired results. 有人可以指出我在这里做错了什么以及如何获得理想的结果。

I have revisited your code a bit and it's working. 我重新访问了你的代码并且它正在运行。 Please try this: 请试试这个:

add_filter( 'woocommerce_add_to_cart_validation', 'only_four_items_allowed_add_to_cart', 10, 3 );
function only_four_items_allowed_add_to_cart( $passed, $product_id, $quantity ) {
    $cart_count = WC()->cart->get_cart_contents_count();
    $total_count = $cart_count + $quantity;

    if ( has_term( 'quantity4','product_cat',$product_id ) && ( $cart_count >= 4 || $total_count > 4 ) ) {
        $passed = false; // Set to false
        $notice = __( "You Chose a Box of 4 Items, Can't Add More", "woocommerce" ); // Notice to display
    }
    elseif ( has_term( 'quantity6','product_cat',$product_id ) && ( $cart_count >= 6 || $total_count > 6 ) ) {
        $passed = false; // Set to false
        $notice = __( "You Chose a Box of 6 Items, Can't Add More", "woocommerce" ); // Notice to display
    }
    if( ! $passed )
        wc_add_notice( $notice, 'error' );

    return $passed;
}

Code goes in function.php file of your active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。

Tested and works. 经过测试和工作。

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

相关问题 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce 根据 WooCommerce 产品类别禁用特定的购物车项目数量字段 - Disable specific cart item quantity fields based on WooCommerce product category 在Woocommerce中仅允许更新一个商品类别的购物车商品数量 - Allow cart item quantity update for only one product category in Woocommerce 折扣基于 WooCommerce 购物车中某个类别的商品数量计数 - Discount based on item quantity count for a category in WooCommerce cart 根据 Woocommerce 中的产品数量替换特定的购物车项目 - Replace specific cart item based on product quantity in Woocommerce Woocommerce中基于产品类别的第二项的数量折扣 - Quantity Discount for 2nd Item Based on Product Category in Woocommerce Woocommerce中基于状态和产品类别的累进购物车项目费用 - Progressive cart item fee based on state and on product category in Woocommerce "基于数量计算的产品类别的购物车折扣" - Cart discount for a product category based on quantity calculations 使用 php(WooCommerce 4.5.1)根据产品的类别和数量添加购物车费用 - Adding cart fees based on product’s category and quantity using php (WooCommerce 4.5.1 ) 购物商品折扣基于Woocommerce 3中的数量 - Cart item discount based on quantity in Woocommerce 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM