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

How to Apply Minimum & Maximum Quantity Rule for Specific Product Attributes in WooCommerce ?

The code snippet below is useful in controlling the quantity the variable products and in specific to a particular attribute. If specific variations of products, characterized by particular attributes like the color ‘Blue’, experience high demand or limited availability, then you can set a lower maximum quantity of 3 limit and a maximum quantity of 6 limit. This ensures that the inventory is distributed among more customers, maximizing sales opportunities.

add_filter( 'woocommerce_available_variation', 'ts_variation_min_max_qty', 10, 3 );

function ts_variation_min_max_qty( $variation_data, $product, $variation ) {
    // Array of variable product IDs to apply quantity limits
    $product_ids = array( 1058, 396, 1062 ); // Update with your desired product IDs

    // Check if the current product ID is in the array of allowed product IDs
    if ( in_array( $product->get_id(), $product_ids ) ) {
        // Get the selected variation attributes
        $attributes = $variation->get_attributes();

        // Check if the 'color' attribute is selected and its value is 'blue'
        if ( isset( $attributes['pa_color'] ) && $attributes['pa_color'] === 'blue' ) {
            // Set the minimum and maximum quantity values for blue color
            $variation_data['min_qty'] = 3;
            $variation_data['max_qty'] = 6;
        }
    }

    return $variation_data;
}

Output

When the customer selects any of the specified variable products in the code with a specific variation attribute with the attribute ‘color’ set to ‘blue’, the code tends to set the minimum quantity to 3 and the maximum quantity to 6 for these variations.

How to Apply Minimum & Maximum Quantity Rule for Specific Product Attributes in WooCommerce ? - Tyche Softwares

If any other variation attribute such as ‘Red’ or ‘Yellow’ is selected, the code will not apply the minimum and maximum quantity limits and the customer can select any number of products.

How to Apply Minimum & Maximum Quantity Rule for Specific Product Attributes in WooCommerce ? - Tyche Softwares

Similarly, you can also set the quantity field to work for all products by setting the minimum and maximum allowable product quantities to be added in WooCommerce cart.

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