Cap the Quantity Selector
Limits the quantity field on product and cart pages to a maximum of 10 per product.
Opens WPIDE on
Code
add_filter( 'woocommerce_quantity_input_args', function ( $args, $product ) {
$max = 10;
if ( ! isset( $args['max_value'] ) || $args['max_value'] <= 0 || $args['max_value'] > $max ) {
$args['max_value'] = $max;
}
return $args;
}, 10, 2 );
Notes
This limits the input field. Quantities added another way (for example by URL) are not validated.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.