I am trying to disable quantity changes in the Woocommerce cart to every product except a single category.
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:
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). It should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.