Limit maximum quantity of free products in Woocommerce cart

My client has asked for the ability to offer free samples of their variable products on their Woocommerce online store, with a maximum of 1 of each unique product sample and 5 of any of the samples on their order.

I’ve achieved part of this by adding an extra “Free Sample” variation to each product and then using a free Min/Max quantity plugin to limit the amount of each individual free sample to 1 per order, please see the following screenshots:

So far I haven’t established a way to limit the maximum number of any combination of the “Free Sample” variations to 5 though. The only way I can see is by limiting the total number of free products (i.e. price = £0) per order to 5, or alternatively by assigning a specific shipping class to each variation (i.e.”Free Samples”) and then somehow limiting the amount of products assigned with this shipping class in each order to 5. Is this possible?




Woocommerce have validation hook which you can use.

First one is when we add to our cart

function add_to_cart_free_samples($valid, $product_id, $quantity) {
    $max_allowed = 5;
    $current_cart_count = WC()->cart->get_cart_contents_count();
    foreach (WC()->cart->get_cart() as $cart_item_key=>$cart_item ){
        // Here change attribute group if needed - currently assigned to default size attribute
        $variation = $cart_item['variation']['attribute_pa_size'];
    if( ( $current_cart_count > $max_allowed || $current_cart_count + $quantity > $max_allowed ) && $variation === 'free-sample' && $valid ){
        wc_add_notice( sprintf( __( 'Whoa hold up. You can only have %d items in your cart', 'your-textdomain' ), $max_allowed ), 'error' );
        $valid = false;

    return $valid;

add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_free_samples', 10, 3 );

Second one is when we update the cart on cart page for example.

function update_add_to_cart_free_samples( $passed, $cart_item_key, $values, $updated_quantity ) {

    $cart_items_count = WC()->cart->get_cart_contents_count();
    $original_quantity = $values['quantity'];
    $max_allowed = 5;
    $total_count = $cart_items_count - $original_quantity + $updated_quantity;
    foreach (WC()->cart->get_cart() as $cart_item_key=>$cart_item ){
        // Here change attribute group if needed - currently assigned to default size attribute
        $variation = $cart_item['variation']['attribute_pa_size'];
    if( $cart_items_count > $max_allowed && $variation === 'free-sample' ){
        $passed = false;
        wc_add_notice( sprintf( __( 'Whoa hold up. You can only have %d items in your cart', 'your-textdomain' ), $max_allowed ), 'error' );
    return $passed;
add_filter( 'woocommerce_update_cart_validation', 'update_add_to_cart_free_samples', 10, 4 );

In $cart_item you can debug and see all info for the current product in the cart. From there you can add condition depending on shipping or attribue or price or w/e you want.

Similar questions

Set quantity minimum, maximum and step at product level in Woocommerce
I'm using "set minimum and maximum allowable product quantities to be added in WooCommerce Cart" to require a minimum and maximum quantity order. Now, i need to impose for several products (not all) a mulitple quantity. For example : 6 bottles, 12 bottles, 18 bottles (multiple of 6) or for other 12 bottles, 24 bottles (multiple of 12) I f...
Display Maximum Quantity in WooCommerce Shop page
I am trying to display Maximum Quantity on the WooCommerce Shop page (home page) I used a [get_max_purchase_quantity()] but I do not think that is, I need just to display the value. like that plz help
In which file in WordPress do I change the "Add to cart" and quantity buttons' aspect? + quantity counter
In which file can I change the form.cart section? Ex, I don't want the "Add to cart" button to be on the same lime as the quantity buttons - and how can I do this? I would also want to add a third section which will calculate the total price automatically depending on the quantity.
Woocommerce change quantity min limit for each product in cart
I have couple of products that can only be bought if min quantity is over certain number. I wrote and put a snippet of code in my child themes functions.php to change the min quantity for products. So what it does is, if it finds a product with the tag 5 then it will change the quantity min to 5. this works in single product page but it doesn't wor...
Disable WooCommerce Payment methods if cart item quantity limit is reached
Is there a way or filter to disable selective payment methods if cart quantity increase more than "X number of items" example "15"? I know we can limit max number of quantity before adding to cart but I want to disable some payment methods only. Thanks
WooCommerce - Buy 10, get 3 free - only the cheapest 3 are free
I have a client currently who sell line-marking paint, and they want an offer on their site that works in the following way: If a customer purchases 10+ paints (they can mix and match), they will receive 3 free, but only the cheapest 3, free... An example is like the below: I am struggling to accomplish this in WooCommerce, despite trying for longe...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.