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

How To Show a Message when the Minimum and Maximum Quantity Rules Applied in WooCommerce?

Showing notice messages on relevant pages helps users to receive the right information and helps them take prompt actions based on the message. For instance, if your store is selling downloadable products then using the function wc_print_notice(), you can easily display a custom message for downloadable products in WooCommerce.

In this post, we will be customizing the quantity input field to select only a minimum of 2 and a maximum of 5 for specific products. Additionally, we will display a notice message on the product page, so the customer immediately knows the quantity constraints associated with that product. This clarity allows them to make informed decisions about how many units to be added to their cart.

function ts_woocommerce_quantity_input_min_callback( $min, $product ) {
    // Array of product IDs for which to set the minimum quantity
    $product_ids = array( 100, 20, 30 ); // Update with your desired product IDs
    
    // Check if the current product ID is in the array
    if ( in_array( $product->get_id(), $product_ids ) ) {
        $min = 2; // Set the minimum quantity to 2 for the specified products
    }

    return $min;
}
add_filter( 'woocommerce_quantity_input_min', 'ts_woocommerce_quantity_input_min_callback', 10, 2 );

/*
* Changing the maximum quantity to 5 for specific WooCommerce products
*/

function ts_woocommerce_quantity_input_max_callback( $max, $product ) {
    // Array of product IDs for which to set the maximum quantity
    $product_ids = array( 100, 20, 30 ); // Update with your desired product IDs
    
    // Check if the current product ID is in the array
    if ( in_array( $product->get_id(), $product_ids ) ) {
        $max = 5; // Set the maximum quantity to 5 for the specified products
    }

    return $max;
}
add_filter( 'woocommerce_quantity_input_max', 'ts_woocommerce_quantity_input_max_callback', 10, 2 );

/*
* Displaying notice for minimum and maximum quantities on the product page
*/

function ts_display_quantity_notices() {
    global $product;

    // Array of product IDs for which to display quantity notices
    $product_ids = array( 100, 20, 30 ); // Update with your desired product IDs

    // Check if the current product ID is in the array
    if ( in_array( $product->get_id(), $product_ids ) ) {
        $min_quantity = 2;
        $max_quantity = 5;

        wc_print_notice( sprintf( __( 'Minimum quantity allowed: %s & Maximum quantity allowed: %s', 'woocommerce' ), $min_quantity, $max_quantity ), 'notice' );
    }
}
add_action( 'woocommerce_before_add_to_cart_button', 'ts_display_quantity_notices' );

Output

When the customer visits the product page of the specific products for which the minimum of 2 and maximum of 5 quantity rule is applied, it clearly conveys the message of the quantity rule being applied to these products as shown below.

How To Show a Message when the Minimum and Maximum Quantity Rules Applied 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