Set a Minimum Order Quantity

May 17, 2019: Add "validate_empty_fields" parameter and set it to true by default. This parameter controls whether fields with not value should show the minimum quantity validation message.

May 17, 2019: Add "validate_empty_fields" parameter and set it to true by default. This parameter controls whether fields with not value should show the minimum quantity validation message.

May 17, 2019: Add "validate_empty_fields" parameter and set it to true by default. This parameter controls whether fields with not value should show the minimum quantity validation message.

May 17, 2019: Add "validate_empty_fields" parameter and set it to true by default. This parameter controls whether fields with not value should show the minimum quantity validation message.

Gravity Forms provides the ability to set a minimum per field when using a separate Quantity field but what about a minimum quantity for the entire order? This snippet can help!

An added bonus! This snippet also allows you to specify a minimum quantity for a single product field without requiring a separate Quantity field.

How do I install this snippet?

Click the “Copy to Clipboard” link at the bottom of the snippet then paste the code into your theme’s functions.php file. Abracadabra!

Do I need to configure this snippet to work with my form?

Yes! This snippet has four bits to customize.

  1. Update the 86 in the filter name gform_validation_86 to the ID of your own form.
  2. Update the $min_qty variable to the minimum quantity your form should require.
  3. Update the $min_qty_message variable to the validation message which should be displayed if the minimum quantity is not met. Use %1$d to populate the minimum number of tickets require. Use %2$d to populate the number of tickets the user must add to their order to meet the minimum. The actual values will be populated when the form is validated.
    Minimum Quantity Validation Message
  4. If you wish to limit the minimum quantity requirement to a specific set of fields, enter the IDs of these fields in the $min_qty_fields = array(); array like so: $min_qty_fields = array(3, 5);. Otherwise, simply leave this variable as is.

Ready to go!

And that’s it; you’re all ready to go. If you use this snippet, we’d love to see it live on your site. Consider sharing a link to your implementation in the comments.

Need to set a maximum order quantity? Let us know and we’ll show you how to modify this snippet to do it!

Comments

  1. Val V.
    Val V. November 1, 2022 at 10:01 am

    Just used the provided snippet and I see one error: the form is correctly submitted, it “talks” to Stripe and gets back to the user visually presenting the custom message about the minimum quantity. However, the charge is made for the 1 piece in the quantity ( I set the minimum required quantity to 2 pieces).

    Any advice?

    Reply
    1. Dario Space
      Dario Space November 1, 2022 at 10:41 am

      Hi Val,

      Does disabling Stripe fix the issue? This will probably require some digging. If it doesn’t work for you, then can you get in touch with us via our support form if you have an active Gravity Perks License.

      Best,

  2. Itshak KALFON
    Itshak KALFON January 18, 2022 at 10:21 am

    Hi – Is this snippet still working with the new 2.5 release ?

    I also use the GF ecommerce template form.

    But the snippet has no effect.

    Reply
  3. Heath Flohre
    Heath Flohre May 21, 2021 at 11:38 am

    Gravity Forms mentions not using the form specific version of filter with forms that also have payment add-on feeds and to use the generic version of the filter and perform the form id check within the hooked function. Would that involve adding an if $form[‘id’] = # statement within the filter code?

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff May 21, 2021 at 12:55 pm

      Hi,

      You can add the if statement at the top of the code, to return if the $form[‘id’] != #. This way the codes attached to the filter will only run if the ID in the $form array matches a specific Form ID.

      Best,

  4. MR KONSTANTIN PULMAN
    MR KONSTANTIN PULMAN February 16, 2021 at 4:58 am

    Hi, Worked perfectly for me The few points I would like to make:

    Min Quantity message appears under each product box, When I have for example 10 products, its gives the message under each of them – not look nice. Can it be set to display on Top right after the main GF error message, as option?

    In the beginning of the code as per bellow, $result = gw_validate_minimum_quantity – appears twice with the same ‘min_qty’ and ‘min_qty_fields’ options – what the second one does? do I need to change it as well to match the same as in first one?

    add_filter(‘gform_validation_123’, function( $result ) {

    $result = gw_validate_minimum_quantity( $result, array(
        'min_qty' => 20,
        'min_qty_message' => 'Customized messaged for this group.',
        'min_qty_fields'  => array( 1, 2, 3 ),
    ) );
    
    $result = gw_validate_minimum_quantity( $result, array(
        'min_qty' => 15,
        'min_qty_fields' => array( 4, 5, 6 ),
    ) );
    
    return $result;
    

    } );

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff February 16, 2021 at 8:48 am

      Hi Mr Pulman,

      Displaying the validation error at the top will require some customizations to the snippet. If you have a Gravity Perks license, drop us a line via our support form and we’ll be happy to look into this. The $result = second gw_validate_minimum_quantity is applying a different minimum quantity to a different set of fields. You can ignore that part if you will be applying the same minimum quantity to all the fields on the form.

      Best,

    2. MR KONSTANTIN PULMAN
      MR KONSTANTIN PULMAN February 16, 2021 at 12:38 pm

      Hi, in my previous post you replied: “$result = second gw_validate_minimum_quantity is applying a different minimum quantity to a different set of fields.”

      is that mean that it’s possible to set Quantity on All fields – AND – For individual Fields?

      as for example if I want to set some fields with minimum Quantity of 2, some fields of minimum Quantity of 4, but in total all of the fields minimum quantity should be 12 ? Can I just add several $results ( third, forth…. etc. )

    3. Samuel Bassah
      Samuel Bassah Staff February 16, 2021 at 1:04 pm

      Hi,

      Yes, you can add several $results. So you will add additional $result = gw_validate_minimum_quantity statement for every set of fields that has a similar minimum quantity.

      Best,

  5. Heath Flohre
    Heath Flohre June 4, 2020 at 2:54 pm

    I do need to set a minimum and maximum. And is there any way to have different error messages for each?

    Reply
    1. Scott Ryer
      Scott Ryer Staff June 4, 2020 at 4:37 pm

      Hi Heath,

      Maximum order quantity isn’t currently supported in the snippet. If you have a Gravity Perks license, drop us a line and we’ll be happy to look into adding support for this.

    2. Heath Flohre
      Heath Flohre June 22, 2020 at 6:10 pm

      The last sentence of your article says “Need to set a maximum order quantity? Let us know and we’ll show you how to modify this snippet to do it!” Do I need a Gravity Perks license to get the modification?

    3. Ryan Donovan
      Ryan Donovan June 22, 2020 at 6:35 pm

      Hello Heath, as Scott has explained before, the maximum order quantity isn’t currently supported in the snippet. With that being said, the simplest way (assuming you don’t need to set a minimum quantity) would be to change this line if($quantity >= $min_qty) to this: if($quantity < $min_qty). This would effectively change the behavior of this snippet to enforce a maximum rather than minimum quantity. Another way you can accomplish this is by using a separate Quantity field you can set a “max” range. If you do need added support for both fields, and you have a Gravity Perks license, you can drop us a line and we’ll be happy to look into adding this into the snippet.

  6. Gert-Jan Buth
    Gert-Jan Buth July 17, 2019 at 4:17 pm

    Hi David,

    Thanks for this great snippet. I have a question similar to Nin0 above (May 2019):

    I have a form with 16 products (Christmas gift for employees/relations), all with a quantity field, and a filter activated to pre-fill the quantity fields with a 0, so visitors won’t have to do this manually for each product they don’t want to order.

    How do I set the quantity validation for the entire form to a minimum of 1? And can I set the location of the validation message to be at the top of the form, just like a regular error message for invalid input (AJAX enabled, so will be visible straight away)?

    Best regards, Gert-Jan

    Reply
  7. Nin0
    Nin0 May 15, 2019 at 6:14 am

    Hello David,

    I have a question about the “$min_qty” that we need to adjust to the minimum quantity. In your script I find 3 places to change this (2 in the filter, 1 in the function) Changing the one in the function doesn’t seems to do anything. Also the “min_qty_fields” can be changed in these 3 places, but only the one in the filter seems to have an effect on the front side.

    Another issue is the one that Rasmus is referring to. What if you have 50 price fields? If one of these fields displays the error, all the other ones (that are empty) displays that error code. I discovered that you can avoid this by duplicating the “$result = gw_validate_minimum_quantity( $result, array(……” and then change the array by the number of the product field. But I guess this is not the proper when having 50 product fields or more. Is there a better way to solve this?

    Thank you, Best regards Nin0

    Reply
    1. David Smith
      David Smith Staff May 15, 2019 at 10:47 am

      Hi Nin0, you only need to edit the code between lines 6 and 20. Nothing inside the gw_validate_minimum_quantity() function needs to changed.

      The issue is that you do need to let the user know which fields impact the minimum quantity. You could put a message at the top but that doesn’t accurately instruct the user on what needs to be interacted with in order to resolve the validation issue.

    2. Nin0
      Nin0 May 15, 2019 at 11:33 am

      Hi David,

      Thank you for the clarification. I have a better view on the procedure and how it works. However, If I have a minimum quantity of 5, that has to reflect on all the individual product fields when a number is filled in. Product fields that are not filled in (left blanc) should not display the error. So when I have 5 product fields and only the first one has a lower number than 5 (and the rest is not filled in) the error message should appear only in the product field that has that lower number. A viewer can choose the fields he wants to use an leave the remaining blanc. Is this possible?

      Many thanks Best regards, Nin0

    3. David Smith
      David Smith Staff May 15, 2019 at 5:30 pm

      I wouldn’t expect the error message to appear on fields that have no quantity specified unless that quantity is specifically set to 0. If this is happening for you, this would be something we’d be happy to look into via support.

    4. Nin0
      Nin0 May 16, 2019 at 10:46 am

      I think it checks the total field and not each product field for its quantity. When I set a minimum quantity of 5, this should apply to each individual product field and not only the total field. Ok for me to continue this via support, but I guess I have to purchase a license first. Is a basic account ok to help me fix these issues?

    5. David Smith
      David Smith Staff May 17, 2019 at 5:00 pm

      Hey Nin0, see the most recent update above regarding the new “validate_empty_fields” parameter. I think this will help you.

    6. Nin0
      Nin0 May 18, 2019 at 4:46 am

      Hi David,

      Thanks for the update but unfortunately it’s not working. I copied the new script and used the new parameter (set it to true and false) but it still displays the error message on all the other empty fields when only one field has a value. Also, as soon as one field has the minimum quantity, the error message is not being displayed if another field did not have the minimum quantity. It does not check each individual field but the total of all fields.

      Best regards, Nin0

    7. David Smith
      David Smith Staff May 19, 2019 at 12:12 pm

      To clarify, It is supposed to check all fields. It’s a minimum order quantity rather than a minimum field quantity.

      As for the validation message, this new version fixes that issue fo me. Validation messages are no longer showing up for me if the field has no value.

      We’ll be happy to dig into this further but it would require and Advanced license (which includes snippet support). Our Basic license does not.

  8. Rasmus
    Rasmus May 14, 2019 at 6:04 am

    Hi David. Thanks for the snippet. If more than one field in the array, I seem to get the message for all products within the array. So if 1 product has error of “You need 5 more”, it also shows the message for the other products in the specified array. So it displays message on not-yet-filled-out quantity fields. Can you help me resolve? :-) Have a nice day, Rasmus

    Reply
    1. David Smith
      David Smith Staff May 17, 2019 at 5:01 pm

      Hi Rasmus, see the most recent update above regarding the new “validate_empty_fields” parameter. I think this will help you.

  9. Edward
    Edward May 4, 2019 at 7:07 am

    Hey David, I have several WC product pages with similar GF forms and they each have several product fields with minimum quantity for the total order. How would you suggest I modify this wonderful snippet to: 1) include the form ID function of several forms so I don’t have to repeat the snippet for each form and 2) where I have 3 product fields in the form with quantity enabled, how can I “sprintf” the min_qty_message once instead of appearing after each field when minimum quantity is not reached,

    http://beta.dinnerup.com/product/acre-thai-bowl-buffet/

    Thanks!

    Reply
    1. David Smith
      David Smith Staff May 6, 2019 at 7:40 am

      Hi Edward, we don’t have the throughput to provide support for snippet customizations but we’re happy to leave your comment here for any other folks who might like to take a stab at this. ?

  10. Eva Szanto
    Eva Szanto April 5, 2019 at 6:42 am

    Hi David,

    I tried to implement this snippet, but it does not work……

    Have a look at the code:

    http://snippi.com/s/v7fq405

    You have to choose first “Zufuhr 24,50 €” in the first place, than

    Kaminholz (25kg-Sack) ID 36 gets visible.

    Can you help?

    Thank you, Eva

    Reply
  11. rich u
    rich u March 22, 2019 at 4:35 pm

    Thankyou for sharing! Is there a way to modify this snippet for a minimum order price? (The kits we sell need to have a minimum $4000 combined to purchase) Have a good day!

    Reply
  12. Dillon
    Dillon April 11, 2018 at 5:42 pm

    Hello, We’re getting an issue where the validation is working on the minimum order total and the form doesn’t actually get submitted or saves an entry but it is still charging the credit card. How would we overcome this?

    Reply
  13. Eddy
    Eddy December 14, 2017 at 5:11 pm

    Hi We trying to run this code to limit the quantity in the product field. The code works fine but the one issue that we are facing is the page won’t load or give us an error message if the product field is not in the legal range. when we load the page and fill out the missing box we are supposed to get a message Variable value is not within the legal range.

    what changes need to make so we get the result.

    thanks

    Reply
  14. Task.Team
    Task.Team November 11, 2017 at 10:17 pm

    Thanks for the great share.

    We are still receiving the ‘Oops! We could not locate your form.’ error.

    Do you have any suggestions?

    Thanks again!

    Reply
  15. Lucie
    Lucie July 17, 2017 at 8:07 pm

    For a minute I thought I had found a solution but the script is returning an “Oops! We could not locate your form” error!

    Reply
  16. Helen
    Helen May 26, 2017 at 1:06 am

    Hi David,

    I am trying to set this up for different forms, with different minimum quantities for various fields. What would the best way be to set this up?

    Reply
  17. james fraser
    james fraser February 22, 2017 at 10:47 am

    Hi David,

    I’ve been trying to get this to work with Woocommerce and Gravity Product Add ons but it is still submitting the product to the cart with though my product quantities are zero. I guess the Woocommerce button is overriding the Gravity forms quantity however, I have 2 other fields that are required such as date and a select field and the validation rules run if they are not selected.

    Any suggestions?

    Reply
    1. David Smith
      David Smith Staff February 25, 2017 at 9:07 am

      Hi James, I would expect this snippet to work with the WC GF Product Add-ons plugin; I’m not sure why it wouldn’t. Maybe another user has encountered this same issue.

  18. James
    James February 10, 2017 at 4:11 pm

    This snippet is exactly what I needed, should be built into Gravity Forms. Been trying to solve this myself for hours.

    Wonderful!

    James

    Reply
  19. Hossein
    Hossein November 23, 2016 at 3:04 pm

    Hey David,

    I am using Ecwid my e-commerce through word press. For some items I need the minimum quantity ordered. Do you know how I can integrate the code with Ecwid?

    Best, Sam

    Reply
  20. Jose Alquijay
    Jose Alquijay November 5, 2016 at 9:47 pm

    Hello, Do you think its possible to use conditional logic on another field (a text field for example) , but based on the quantity?. For example to display a field if the quantity is bigger than 3?, something like that?. I tried using normal conditional logic but apparently quantity fields are not in the option for conditional logic. Thanks

    Reply
    1. David Smith
      David Smith Staff November 9, 2016 at 5:14 pm

      Hi Jose, you can capture the quantity field in a Number field with Calculations enabled and then based the conditional logic on that field.

  21. Sand0r
    Sand0r August 20, 2016 at 2:25 pm

    Hi there, i’m using this little snippet for the first time and it’s exactly what i needed however, using $min_qty_fields = array(124, 137, 233); will validate all ID’s when only one ID is filled in.

    Is it possible to only validate the ID that’s been filled in?

    Thanks in advance Sander

    Reply
    1. David Smith
      David Smith Staff August 21, 2016 at 10:47 am

      Hi Sandor, if you include multiple fields, they’re considered part of the same “group” and so if one fails validation, they will all fail validation. You can, however, copy and paste this snippet multiple times to configure each field individually. Just make sure you rename the function (i.e. gform_validate_minimum_quantity becomes gform_validate_minimum_quantity1, gform_validate_minimum_quantity2, etc).

  22. guilamu
    guilamu July 5, 2016 at 8:22 am

    Hello, I want to set a minimum price (and not disable the submit button if the price is not high enough).

    Let’s say users are paying for stuff, and if the total is under 30 usd, then it’ll display 30 usd and the user will have at least to pay 30 usd.

    Is there any way to do that?

    Regards,

    Reply
  23. Danny
    Danny May 26, 2016 at 3:44 pm

    This was exactly what I needed! I’m using this on a order form with independent, and optional, products. Is there a way that if the quantity is left blank then the snippet will be nulled? Currently it’s forcing the customer to order the minimum amount even if they don’t need the product.

    Thanks!

    Reply
    1. David Smith
      David Smith Staff July 1, 2016 at 9:30 pm

      Hi Danny, sorry for the slow response. I’ve just updated the snippet to address this issue.

  24. Gonçalo
    Gonçalo May 4, 2016 at 6:38 am

    Hi.

    First, thank you very much for this snippet.

    Please let me know if is possible to set a minimum quantity for two products, for example: We have Product A + Product B and we want to set a minimum quantity of 20 for both, like 10 products A + 10 products B, or 5A + 15B or something else.

    Thanks :)

    Reply
    1. David Smith
      David Smith Staff May 18, 2016 at 10:11 am

      Hi Goncalo, this snippet only supports setting a minimum order quantity; however, you can create separate Quantity fields for each product and there is an option to set a minimum for each there.

  25. Cliff
    Cliff April 25, 2016 at 2:29 pm

    Hey there, I’m looking for a way to set a total form price minimum. I have multiple items, and have already set minimum quantities. Now I would like the form to not be able to be submitted before a total of $300 is chosen from my products.

    Would modifying this code be a good place to start? Thanks for any help you can give!

    Reply
  26. Mike
    Mike March 7, 2016 at 3:03 pm

    Is it possible to modify this to force a maximum quantity on a specific field?

    I have a form that I don’t want a client to be able to checkout if there’s more than 2 in the quantity field.

    I tried to figure it out, but couldn’t.

    Reply
    1. Mike
      Mike March 8, 2016 at 12:53 pm

      My problem is that I’m using a Product field (not just a Quantity field), so I don’t have options for the max value.

      Hoping I can adapt this script to close that loophole.

    2. Mike
      Mike March 8, 2016 at 1:03 pm

      I think I don’t need to now in playing with this.

      I’ll map a quantity field to the Product field and do my shipping calculation math on that.

      Thanks!

  27. Adam Kellett
    Adam Kellett December 9, 2015 at 8:33 pm

    This is exactly what I needed! Thank you for this, it’s working as it is supposed to. I would like to know if it is possible to have one field that increases the quantity by more increments. Example: Donut Flavor1 = +/- 1 Donut Flavor2 = +/- 1 Assorted Dozen Donuts = +/- 12

    I have my minimum set to 12 (dozen donuts minimum order) But I also have a field that lets people order a full dozen (called assorted dozen). This field obviously costs more, but only increases the calculation of this script by 1. I would like to tell the script that if this ONE field is greater than 1, that means they have met the minimum requirement of 12 donuts. If there is a way to increase the total by increments of 12 when this field is used, that would work best.

    Reply
  28. Shonari
    Shonari November 18, 2015 at 7:48 am

    Hi David

    Thanks for the code. A few issues.

    I wanted to apply a minimum quantity for a few fields. I added multiple field ids in the array section. It works except that even if the quantity field is blank its still requests that minimum quantity. The way i’d like it to work is, if the field is blank do nothing but if the quantity is greater than zero but less than minimum order quantity display error.

    Also, it seems to tally the entire array of fields rather than individual fields. So if the minimum quantity set is 20 and three fields have quantities of 3 it says “You must order a minimum of 20 . Order 11 more to complete your order.” Rather than saying for each field “You must order a minimum of 20 . Order 17 more to complete your order.”

    Thanks!

    Reply
    1. Shonari
      Shonari November 18, 2015 at 7:50 am

      The main thing is how do I exclude a blank field (quantity of zero)? But if soemone adds a value thats less than minimum quantity but greater than blank (zero) display error.

  29. Jason
    Jason August 2, 2015 at 8:52 pm

    Hi David – Nice Snippet!

    Is there a way to modify this to set a minimum total for the amount? I am setting up a form that lets you customise your own subscription pack, made up of lots of little add-ons – but it needs to be above a certain dollar amount before it is worthwhile.

    Thanks!

    Reply
    1. David Smith
      David Smith Staff August 3, 2015 at 12:18 pm

      Hi Jason, Richard (from Gravity Forms) wrote a handy snippet that lets you apply conditional logic based based on the form total. If you need to apply conditional logic based on the total of a specific field and it’s options, you can use a Number field with calculation enabled to calculate that total and apply the conditional logic based on that Number field.

  30. Jesper
    Jesper January 2, 2015 at 8:59 am

    how must I change the code to use it with more than one form? I have tried to put the code just 2x times in the function.php, but then I get a ERROR;-)

    Reply
    1. Jesper
      Jesper January 2, 2015 at 1:56 pm

      Hi David, thanks, I do not know how to use this link to class-based snippet ? I have change the ID in the first line: add_filter(‘gform_validation_8’, ‘gform_validate_minimum_quantity’);

      The second form have id=23, but how can I the use the code class-based, to include this form id too?

      Best Regards;-)

    2. David Smith
      David Smith Staff January 2, 2015 at 3:15 pm

      Sorry, Jesper. Forgot this is not a class-based snippet (one of my older ones). You’d need to duplicate all the code and change the form ID in the filter name. I would also consider adding the form ID to the function name to more easily distinguish them. Here’s a simplified example:

      http://pastie.org/9810069

    3. Jesper
      Jesper January 3, 2015 at 8:42 am

      Perfect, now I understand;-) I have now 4 forms with this snippet, working perfect! Thank you so much David.

  31. Shonari
    Shonari December 8, 2014 at 8:49 am

    Great little snippet. I applied this to a specific set of fields but even if the quantity is zero the error comes up. How do I set it that the error comes up ONLY if the user enters a value? IE a blank field or zero value is ok but if they enter any other number (greater than zero but less that minimum value) the error is shown

    Cheers!

    Reply
  32. Shonari
    Shonari November 18, 2014 at 1:01 pm

    Works great. Thanks for this code.

    What if you wanted the function to appear on multiple fields BUT only if they fill the field out? At the moment a visitor isnt required to fill out the field (ie zero) but if they enter a value less than four but greater than blank (zero) the error should come up. At the moment it comes up on everything (event the things they dont want to order)

    Here is a screenshot: http://i.imgur.com/rrBgwIh.png

    Reply
    1. David Smith
      David Smith Staff February 28, 2014 at 10:23 am

      Hi Jennifer, the simplest way (assuming you don’t need to set a minimum quantity) would be to change this line if($quantity >= $min_qty) to this: if($quantity < $min_qty). This would effectively change the behavior of this snippet to enforce a maximum rather than minimum quantity.

  33. Allex
    Allex August 22, 2013 at 10:56 pm

    How if I want to modify this snippet, so it limit maximum and minimum in number / other field based on user meta

    What I want is to make withdraw points form, and maximum limit amount is based an user meta field

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.

Download Snippet

Set a Minimum Order Quantity

This field is for validation purposes and should be left unchanged.