Gravity Wiz

Magically enhanced tutorials, snippets and plugins for Gravity Forms!

  • Gravity Perks
    • Gravity Perks
    • Tutorials & Snippets
    • About
  • Support
    • Documentation
    • Support
    • Account

Set a Minimum Order Quantity

Last updated August 21, 2019 | Written by David Smith 80 Comments

  • 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.

  • April 5, 2019: Fixed various issues with snippet.

  • July 18, 2017: Fixed issue where $result was not returned.

  • July 1, 2016: Fixed issue where if no quantity was specified, minimum order validation was still applied. To force a quantity to be specified, make the Product or Quantity field required.

Show All Updates

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!

Show CodeDownload Code
<?php
/**
* Gravity Wiz // Gravity Forms // Minimum Order Quantity
* http://gravitywiz.com/set-a-minimum-order-quantity/
*/
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;
} );
function gw_validate_minimum_quantity( $result, $args = array() ) {
$args = wp_parse_args( $args, array(
'min_qty' => 1,
'min_qty_message' => 'You must order a minimum of %1$d tickets. Order %2$d more tickets to complete your order.',
'min_qty_fields' => array(),
'validate_empty_fields' => false,
) );
$min_qty = $args['min_qty'];
$min_qty_message = $args['min_qty_message'];
$min_qty_fields = $args['min_qty_fields'];
/* no need to edit below this line */
$form = $result['form'];
$quantity = 0;
$qty_fields = array();
$has_entered_value = false;
foreach($form['fields'] as &$field) {
// if $min_qty_fields specified, make sure only applicable quantity fields are totaled
if(!empty($min_qty_fields) && !in_array($field['id'], $min_qty_fields))
continue;
if(in_array(RGFormsModel::get_input_type($field), array('singleproduct', 'calculation'))) {
// check if product field has separate quantity field, if so skip
if(sizeof(GFCommon::get_product_fields_by_type($form, array("quantity"), $field['id'])) > 0)
continue;
$value = rgpost( "input_{$field['id']}_3" );
if( ! rgblank( $value ) ) {
$has_entered_value = true;
}
$quantity += floatval( GFCommon::clean_number( $value ) );
$qty_fields[] =& $field;
} else if($field['type'] == 'quantity') {
$value = rgpost( "input_{$field['id']}" );
if( ! rgblank( $value ) ) {
$has_entered_value = true;
}
$quantity += floatval( GFCommon::clean_number( $value ) );
if( ! rgblank( $value ) || $args['validate_empty_fields'] ) {
$qty_fields[] =& $field;
}
}
}
if( ! $has_entered_value || $quantity >= $min_qty ) {
return $result;
}
for($i = 0; $i < count($qty_fields); $i++) {
$qty_fields[$i]['failed_validation'] = true;
$qty_fields[$i]['validation_message'] = sprintf($min_qty_message, $min_qty, $min_qty - $quantity);
}
$result['is_valid'] = false;
$result['form'] = $form;
return $result;
}
view raw gw-gravity-forms-minimum-order-quantity-deux.php hosted with ❤ by GitHub
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!

Did this resource help you do something awesome with Gravity Forms? Then you'll absolutely love Gravity Perks; a suite of 32+ essential add-ons for Gravity Forms with support you can count on.

  • View All Perks
  • Buy Gravity Perks

Filed Under: Snippets pricing fields product field quantity field

Comments

  1. Heath Flohre says

    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
    • Scott Buchmann says

      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.

    • Heath Flohre says

      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?

    • Ryan Donovan says

      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.

  2. Gert-Jan Buth says

    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
    • David Smith says

      July 17, 2019 at 5:22 pm

      Hi Gerg-Jan, you would need to duplicate this for each quantity field:

      http://snippi.com/s/obxi8xq

  3. Vi Wickam says

    June 30, 2019 at 8:15 pm

    Is this plugin supposed to work with stock gravity forms, or does it require your plugins?

    Thanks, Vi

    Reply
    • David Smith says

      July 1, 2019 at 6:23 pm

      Hi Vi, this is a stand alone snippet and only requires Gravity Forms.

    • Vi Wickam says

      July 1, 2019 at 6:30 pm

      Sweet! Thanks so much for publishing this helpful resource!

    • David Smith says

      July 2, 2019 at 7:54 am

      Happy to help, Vi. :)

  4. Nin0 says

    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
    • David Smith says

      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.

    • Nin0 says

      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

    • David Smith says

      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.

    • Nin0 says

      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?

    • David Smith says

      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.

    • Nin0 says

      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

    • David Smith says

      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.

  5. Rasmus says

    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
    • David Smith says

      May 14, 2019 at 12:11 pm

      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. ?

    • David Smith says

      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.

  6. Edward says

    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
    • David Smith says

      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. ?

  7. Mark Hannon says

    April 30, 2019 at 1:49 pm

    How do you target a specific form with this code?

    Reply
  8. Eva Szanto says

    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
    • David Smith says

      April 5, 2019 at 8:14 am

      I’ve updated the snippet with a few fixes. Try again. ?

  9. rich u says

    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
    • David Smith says

      March 22, 2019 at 5:15 pm

      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.

    • rich u says

      March 22, 2019 at 7:58 pm

      Is there any modified code you could recommend? :)

  10. Dillon says

    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
    • David Smith says

      April 11, 2018 at 8:35 pm

      Hi Dillon, what payment add-on are you using?

    • Dillon says

      April 11, 2018 at 10:28 pm

      authorize.net

  11. Eddy says

    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
    • David Smith says

      December 15, 2017 at 8:35 am

      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.

  12. Task.Team says

    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
    • David Smith says

      November 13, 2017 at 8:52 am

      This typically occurs when the form ID is not correct.

« Older Comments

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories

  • How To (64)
  • News (21)
  • Plugins (14)
  • Releases (7)
  • Resource (3)
  • Snippets (58)
  • Tutorials (57)
  • Updates (104)

Recent Posts

  • How to Send a Follow-Up and Pre-Fill Information
  • How to Update Posts with Gravity Forms
  • Gravity Wiz Weekly #104
  • The Complete Guide to Using Gravity Forms With Zapier
  • Gravity Wiz Weekly #103

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2021 · Powered by WordPress · Gravity Wiz LLC

  • Support
  • Affiliates
  • About
  • Sitemap
  • Gravity Perks
    ▼
    • Gravity Perks
    • Tutorials & Snippets
    • About
  • Support
    ▼
    • Documentation
    • Support
    • Account