[英]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.