简体   繁体   English

在Woocommerce中仅允许更新一个商品类别的购物车商品数量

[英]Allow cart item quantity update for only one product category in Woocommerce

I am trying to disable quantity changes in the Woocommerce cart to every product except a single category. 我正在尝试禁用Woocommerce购物车中除单个类别之外的所有产品的数量更改。

I have tried a custom function to do this but it disables it for all items. 我已经尝试过使用自定义函数来执行此操作,但是对所有项目都将其禁用。 Looking to only allow one category product quantity to be changed. 希望只允许更改一种类别的产品数量。 I have used this code to disable it on all products so far: 到目前为止,我已使用此代码在所有产品上将其禁用:

add_filter( 'woocommerce_cart_item_quantity', 'wc_cart_item_quantity', 10, 3 );
function wc_cart_item_quantity( $product_quantity, $cart_item_key, $cart_item 
){
    if( is_cart() ){
        $product_quantity = sprintf( '%2$s <input type="hidden" 
name="cart[%1$s][qty]" value="%2$s" />', $cart_item_key,             
$cart_item['quantity'] );
    }
    return $product_quantity;
}

You can use has_tem() conditional function to target only specific product category(ies) set for your cart items like: 您可以使用has_tem()条件函数来仅定位为购物车项目设置的特定产品类别,例如:

add_filter( 'woocommerce_cart_item_quantity', 'wc_cart_item_quantity', 10, 3 );
function wc_cart_item_quantity( $product_quantity, $cart_item_key, $cart_item ){
    $categories = array('disc'); // Define your allowed product category(ies)

    if( is_cart() && ! has_term( $categories, $taxonomy, $cart_item['product_id'] ){
        $product_quantity = sprintf( '%2$s <input type="hidden" 
name="cart[%1$s][qty]" value="%2$s" />', $cart_item_key,             
$cart_item['quantity'] );
    }
    return $product_quantity;
}

Code goes in functions.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的functions.php文件中。 It should work. 它应该工作。

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

相关问题 在 Woocommerce 中一次只允许购物车中的一个产品类别 - Allow only one product category in cart at once in Woocommerce 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce Woocommerce 中定义的产品类别中只允许购物车中的一件商品 - Allow only one item in cart from defined product categories in Woocommerce 根据Woocommerce中的产品类别将最大商品数量添加到购物车 - Max item quantity added to cart based on product category in Woocommerce 根据 WooCommerce 产品类别禁用特定的购物车项目数量字段 - Disable specific cart item quantity fields based on WooCommerce product category WooCommerce:仅获取定制产品的购物车商品数量 - WooCommerce: Get the cart item quantity of a customized product only 购物车中每个产品类别只允许一个产品 - Allow only one product per product category in cart 只允许为 Woocommerce 中的特定产品类别购买一种产品 - Allow only one product purchase for a specific product category in Woocommerce Woocommerce:如何在购物车中只允许一种产品类型? - Woocommerce: How to allow only one product type in a cart? 为特定WooCommerce产品类别的购物车项目设置最小数量 - Set a minimum quantity for cart items from specific WooCommerce product category
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM