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.
- Update the
86
in the filter namegform_validation_86
to the ID of your own form. - Update the
$min_qty
variable to the minimum quantity your form should require. - 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. - 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!
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?
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,
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.
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?
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,
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 ) {
} );
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,
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. )
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,
I do need to set a minimum and maximum. And is there any way to have different error messages for each?
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.
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?
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.
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
Hi Gerg-Jan, you would need to duplicate this for each quantity field:
http://snippi.com/s/obxi8xq
Is this plugin supposed to work with stock gravity forms, or does it require your plugins?
Thanks, Vi
Hi Vi, this is a stand alone snippet and only requires Gravity Forms.
Sweet! Thanks so much for publishing this helpful resource!
Happy to help, Vi. :)
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
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.
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
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.
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?
Hey Nin0, see the most recent update above regarding the new “validate_empty_fields” parameter. I think this will help you.
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
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.
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
Hi Rasmus, this snippet is provided as-is; however, if you’re a Gravity Perks customer, this is something we’d be happy to take a look at via support. ?
Hi Rasmus, see the most recent update above regarding the new “validate_empty_fields” parameter. I think this will help you.
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!
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. ?
How do you target a specific form with this code?
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
I’ve updated the snippet with a few fixes. Try again. ?
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!
We recommend Gravity Forms eCommerce Fields. Add a Number field to your form, use the special
{subtotal}
merge tag as the formula, then base the Submit button conditional logic on the value of that Number field.Is there any modified code you could recommend? :)
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?
Hi Dillon, what payment add-on are you using?
authorize.net
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
Hi Eddy, I’ve just tested the snippet locally and wasn’t able to recreate this issue. If you’re a Gravity Perks customer, we’ll be happy to provide additional support via the support form.
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!
This typically occurs when the form ID is not correct.
For a minute I thought I had found a solution but the script is returning an “Oops! We could not locate your form” error!
Try now, Lucie. Snippet has been updated.
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?
Hi Helen,
You can bind to multiple forms like so:
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?
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.
This snippet is exactly what I needed, should be built into Gravity Forms. Been trying to solve this myself for hours.
Wonderful!
James
Lovely! Glad we could help. :)
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
Hi Sam, sorry, I do not. :(
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
Hi Jose, you can capture the quantity field in a Number field with Calculations enabled and then based the conditional logic on that field.
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
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).
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,
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!
Hi Danny, sorry for the slow response. I’ve just updated the snippet to address this issue.
Not a problem at all! I appreciate you updating the code, thank you!!
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 :)
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.
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!
Hi Cliff, use this snippet to add a Subtotal to your form. You can then use conditional logic to show/hide the submit button based on the value of your calculated subtotal field.
https://gravitywiz.com/subtotal-merge-tag-for-calculations/
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.
Hi Mike, if you use a separate Quantity field you can set a “max” range (screenshot).
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.
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!
Perfect. That is what I was thinking. :)
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.
Hi Adam, you might find this snippet more aligned to your needs: https://gravitywiz.com/rounding-increments-gravity-forms/
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!
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.
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!
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.
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;-)
Hi Jesper, this resource might help: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/ Keep in mind, you’ll only include one copy of the class itself and then you can instantiate it multiple times.
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;-)
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
Perfect, now I understand;-) I have now 4 forms with this snippet, working perfect! Thank you so much David.
Glad to help, Jesper. :)
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!
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
Hey,
Could you let me know how to adapt this snippet, so that I can set a maximum order quantity.
Thanks a lot!
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.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