Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Apply Different Step Quantity for Two Different Products in WooCommerce?

By default, WooCommerce lets you increase the product quantity on a single step count. To override this functionality here comes our handy code snippet that helps to customize the product quantity steps for two different products. For instance, the quantity steps of Product A gets increased in the step counts as 3,6,9,12….etc. And then the quantity steps of Product B gets increased in the step counts as 6,12,18,24…etc.

add_filter( 'woocommerce_quantity_input_args', 'ts_woocommerce_quantity_selected_number', 10, 2 );

function ts_woocommerce_quantity_selected_number( $args, $product ) {
    if ( ! is_cart() ) {
        // Check for product IDs and set step accordingly
        if ($product->get_id() == 100){ // Replace 123 with the actual product ID
            $args['input_value'] = 3; // Start from this value (default = 1)
            $args['max_value'] = 18; // Maximum quantity (default = -1)
            $args['min_value'] = 3; // Minimum quantity (default = 0)
            $args['step'] = 3; // Increment or decrement by this value (default = 1)
        } elseif ($product->get_id() == 470) { // Replace 456 with the actual product ID
            $args['input_value'] = 6; // Start from this value (default = 1)
            $args['max_value'] = 24; // Maximum quantity (default = -1)
            $args['min_value'] = 6; // Minimum quantity (default = 0)
            $args['step'] = 6; // Increment or decrement by this value
        }
    } else {
        // Check for product IDs and set step accordingly for cart
        if ($product->get_id() == 100) { // Replace 123 with the actual product ID
            // Cart's 'min_value' is 0
            $args['max_value'] = 18;
            $args['step'] = 3;
            $args['min_value'] = 3;
        } elseif ($product->get_id() == 470) { // Replace 456 with the actual product ID
            $args['max_value'] = 24;
            $args['step'] = 6;
            $args['min_value'] = 6;
        }
    }

    return $args;
}

Output

Let’s consider that the customer visits the product pages of high-value item product A (product id:100) as specified in the code. When the customer tries to increase the product quantity the quantity levels are only increased in the step count of 3,6,9,12….The customer only be able to select any quantity among thee step count values.

How to Apply Different Step Quantity for Two Different Products in WooCommerce? - Tyche Softwares

Suppose the customer visits the product page of this product B(product ID: 470) as specified in the code, then the step count will be increased in the quantity levels of 6,12,18,24……

How to Apply Different Step Quantity for Two Different Products in WooCommerce? - Tyche Softwares

Instead of increasing the product quantity in step counts you can simply set minimum and maximum allowable product quantities to be added to the WooCommerce cart by creating these fields in the product data of the admin backend section.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x