When online store owners want to restrict control over the product quantities for a particular category, then this code snippet is a handy solution for them. The code helps to fix the minimum and maximum quantity for products belonging to a particular category. For example, in the code we have specified the category slug of ‘tshirts’ and ‘electronics’ and the min-max rule will be set for products of these categories only.
add_filter( 'woocommerce_quantity_input_args', 'ts_min_max_qty_simple_products', 10, 2 ); function ts_min_max_qty_simple_products( $args, $product ) { $allowed_categories = array( 'tshirts', 'electronic' ); // Replace 'category-slug-1' and 'category-slug-2' with your category slugs // Check if the product is in one of the allowed categories if ( has_term( $allowed_categories, 'product_cat', $product->get_id() ) ) { // If the product is in one of the allowed categories, set min and max values $args['min_value'] = 3; $args['max_value'] = 6; } return $args; }
Output
When a customer visits the product page of these categories of ‘tshirts’ and ‘electronics’, then the fixed quantity rule of minimum value of 3 and maximum value of 6 as specified in the code will be set. Customers will not be able to increase/decrease apart from the specified minimum and maximum value.
Similar to the above min-max quantity rule, you set minimum and maximum allowable product quantities to be added in WooCommerce cart by creating these fields in the product data section of the admin backend.