When you run an online WooCommerce store, you can control how customers buy your products by setting predefined quantity steps. For example, if you sell food items such as coffee sachets, tea bags, instant noodle packs, you can predefine the product quantities for these items in fixed steps like 10, 20, or 30. This customization ensures that customers don’t just buy a single item but always purchase in bulk.
Previously, we explored the useful feature of adding a quantity field to both the classic and the new WooCommerce checkout blocks page, where it is missing by default. Now, we will enhance this functionality by setting a fixed step quantity for specific product categories on both the product page and the new cart blocks page. This is especially useful for store owners who want certain product categories to be sold in bulk rather than as single units.
Solution: Add Step Quantity to the WooCommerce Product Page and Cart Blocks Page
When a customer visits the product page of an item from the specific category ‘food’, the quantity will automatically be set to 10. If they try to adjust it, they can only select quantities in multiples of 10.
The same rule applies when they proceed to the WooCommerce Cart Blocks page.
add_filter( 'woocommerce_quantity_input_args', 'ts_woocommerce_quantity_selected_number', 10, 2 ); function ts_woocommerce_quantity_selected_number( $args, $product ) { // Get product categories $categories = wp_get_post_terms( $product->get_id(), 'product_cat', array( 'fields' => 'slugs' ) ); // Check if the product belongs to the 'electronic' category if ( array_intersect( ['electronics'], $categories ) ) { $args['input_value'] = 10; // Start from this value $args['step'] = 10; // Increment or decrement by this value $args['min_value'] = 10; // Ensure the minimum value is 10 } return $args; } add_filter( 'woocommerce_store_api_product_quantity_multiple_of', function( $value, $product, $cart_item ) { $categories = wp_get_post_terms( $product->get_id(), 'product_cat', array( 'fields' => 'slugs' ) ); if ( array_intersect( ['electronics'], $categories ) ) { return 10; } return $value; }, 10, 3 );
Output
When the customer proceeds to the cart, the quantity field will remain set to the predefined value of 10, and any adjustments will follow the same step-based increments.
We have seen how to set step quantities for specific product categories, as this helps you to enforce bulk purchases. Additionally, some businesses may need more flexibility, such as allowing decimal quantities for products sold by weight, length, or volume. In such cases, consider this valuable customization that enables the product quantities and stock in decimals for WooCommerce products.