Gravity Wiz

Magically enhanced tutorials, snippets and plugins for Gravity Forms!

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

Better Inventory with Gravity Forms

Gravity Forms does not support the concept of inventory out of the box. This snippet provides an easy method for setting up simple, one-off inventory limits on a per field basis.

Last updated November 10, 2020 | Written by David Smith 529 Comments

  • November 10, 2020: Migrated to Snippet Library. Fixed an issue where Auth.net would still charge for the submission even when validation error was returned.

  • October 18, 2019: Fixed issue where field groups were not correctly limited on submission.

  • October 10, 2019: Fixed issue where Calculated Product fields were still added to order summary after their inventory was exhausted.

  • March 19, 2019: Fixed issue where remaining shortcode did not always target correct instance of Better Inventory.

  • November 17, 2018: Added support for fetching prepopulated values when limiting by field group.

  • April 26, 2018: Updated snippet for Gravity Forms 2.3; snippet now requires 2.3.

  • May 31, 2017: Updated "remaining" shortcode to correctly limit by field groups and approved payments flag.

  • January 27, 2017: Fixed bug with approved_payments_only parameter where entries with no payment were counting towards all inventory limits.

  • January 21, 2017: Added support for automatically limiting GP Limit Date-enabled Date fields and paired with an inventory input (via field_group parameter) so that days with exhausted inventory or not available.

  • January 17, 2017: Updated "remaining" shortcode to dynamically retrieve the limit for the specified form and field.

  • March 23, 2016: Added support for specifying "stock_qty" parameter as a function. Allows for dynamically setting the stock_qty during runtime.

  • March 15, 2016: Updated paid status to "Paid" from "Approved" to work with GF Payment Add-on framework.

  • October 5, 2014: Added support for "remaining" action on [gravityforms] shortcode.

  • October 1, 2014: Added support for "sum" action on [gravityforms] shortcode.

  • November 11, 2012: Fixed issue where database prefix was not applied via $wpdb->prefix.

Show All Updates
View CodeDownload Code

Let’s dive into the code first.

How do I install this snippet?

Easy peasy. Just copy and paste the code above into your theme's functions.php file.

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

You don’t have to modify the snippet itself. Just the parameters with which you initialize it.

new GW_Inventory( array(
    'form_id'                  => 123,
    'field_id'                 => 4.3,
    'stock_qty'                => 20,
    'out_of_stock_message'     => 'Sorry, there are no more tickets!',
    'not_enough_stock_message' => 'You ordered %1$s tickets. There are only %2$s tickets left.',
    'approved_payments_only'   => false,
    'hide_form'                => false,
    'enable_notifications'     => true
) );

Parameters

  • $form_id the ID of the form you are working with.
  • $field_id the ID of your product or quantity field. If using a Single Product field, you’ll need to specify the quantity input ID which will always be {field ID}.3 (ie if the field ID is 12, the quantity input ID will be 12.3). If using a separate Quantity field, simply specify the field ID.
  • $stock_qty the number of this item you have available.
  • $out_of_stock_message the message which should be displayed to users when the product limit has been reached. You can get fancy with this and add HTML as well. Be sure to escape any double quotes with a backslash. Here’s a more advanced example:
    $sum_limit_message = '<div style="border: 1px solid #e6db55; background-color: #FFFFE0; padding: 10px;">Sorry, this show is sold out.</div>';
    Better Inventory: Limit Message
  • $not_enough_stock_message the message which should be displayed on the field if the limit has not been reached, but the user’s requested quantity would exceed the product limit.
    Better Inventory: Limit Validation Message
  • $approved_payments_only true/false indicate whether all submissions for this field should be counted against the limit or only submissions with an approved payment. You’ll want to be careful here when using PayPal Standard due to the delay between the form submission and the payment being approved. It could lead to users exceeding the limit.
  • $hide_form by default this snippet will only hide the field when the limit has been reached. If you would rather hide the entire form, set this to true.
  • $enable_notifications enable the ability to create notifications that are sent when the stock of a product (or any configured field) has been exhausted

Shortcodes

  • Remaining
    Show the quantity remaining for the specified input and limit. [gravityforms action="remaining" id="1" input_id="2.3" limit="50"]

  • Sum
    Show the current sum of quantities ordered for this field across all entries. [gravityforms action="sum" id="1" input_id="2.3"]

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

Comments

  1. Casey says

    November 4, 2019 at 7:45 pm

    Can this be used on one form with multiple fields? I’ve set up a shopping cart and would like to preload inventory for each of the items. Additionally, each week this form is cloned into a new one – all with new inventory items.

    Reply
  2. JEREMY W CARIS says

    October 30, 2019 at 12:58 pm

    How can I apply the total stock limit to more than one field? In other words, I have two single product fields, but I want the total inventory of 100 to apply to the total of the combination of the fields. If I sell 80 and 20, or if I sell 50 and 50, then both should be out of inventory.

    Reply
    • David Smith says

      November 4, 2019 at 7:20 pm

      Hi Jeremy, this snippet doesn’t support this in a great way but you could use a Number field to calculate the sum of both product quantities and then apply the inventory on that field. The downside to this is that the validation error would be on this “Total Quantity” field and not the individual product fields.

  3. Benjamin says

    September 23, 2019 at 1:55 pm

    Is there a way to add a out of stock css class to the field Id, for exemple deactivate the field?

    Reply
    • David Smith says

      September 23, 2019 at 2:09 pm

      Hi Benjamin, by default, the field will be “deactivated” and replaced by the out of stock message.

    • benjamin says

      October 22, 2019 at 10:41 am

      Your right… thanks a lot.

      Another question.

      I use the Many Different Forms, Same Settings snippet. but only one of them work ( the first one ) and when the first one work, it change the value for both of them…

      There is my snippet

      class GWLimitBySum extends GW_Inventory { }

      foreach( array( 3,9 ) as $form_id ) { new GW_Inventory( array( ‘form_id’ => $form_id, ‘field_id’ => 63, ‘stock_qty’ => 16, ‘out_of_stock_message’ => ”, ‘not_enough_stock_message’ => ”, ‘approved_payments_only’ => false, ‘hide_form’ => false, ‘enable_notifications’ => false ) );

    • David Smith says

      November 4, 2019 at 4:46 pm

      The code looks good and a similar setup is working for me locally… I don’t have any ideas about what might be going wrong for you. We do provide additional support for our Advanced and Pro Gravity Perks license holders.

  4. Jeroen says

    September 3, 2019 at 8:40 am

    Does this code: [gravityforms action="remaining" id="1" input_id="x.x" limit="xx"] change the items remaining only after approved_payments? I have set this parameter in functions.php “true”

    Reply
    • David Smith says

      September 6, 2019 at 8:50 am

      You would need to set the approved_payments_only parameter when you’re initializing the GW_Inventory class.

  5. Mary says

    August 3, 2019 at 11:38 am

    Hi, Is there also a code if I will make the stock conditional? So for instance for a dance workshop I need to have the same number of male and female participants. Is there a way to manage this in gravity forms? Greetings Mary

    Reply
    • David Smith says

      August 5, 2019 at 8:42 am

      Hi Mary, your best bet is to simply have two separate product fields for each workshop; one for males and one for females. In this way, you could maintain individual stock for each one.

  6. Tayler says

    July 25, 2019 at 11:51 am

    Hi There,

    I have 4 different forms I need to add quantity limits to. Is there a way to make that happen without copying all 400+ lines of code 4 times? Can I throw an array of form and field ids into the associative array?

    Thanks, Tayler

    Reply
    • David Smith says

      July 25, 2019 at 11:52 am

      Hi Tayler, this will help:

      https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/

    • Tayler says

      July 25, 2019 at 12:00 pm

      Ok, that does help, except that in the full code snippet I see this: http://snippi.com/s/tfy8jgz

      There aren’t any visible classes to begin with, so where do I place those within the $args object?

    • David Smith says

      July 25, 2019 at 12:46 pm

      If you look at the very bottom of the snippet you will see where the class is initialized. That is the code that should be duplicated.

  7. Francesco says

    July 1, 2019 at 2:38 am

    Hy guys!

    one month ago I used without any problem the plugin on a website, today I’m implementing the same code on another site but after copy past the code on functions.php the site goes down and report the sentence “the website has technical problems”.

    If I remove the code, the site return ok.

    Any idea?

    Reply
    • David Smith says

      July 1, 2019 at 10:46 am

      There are no known conflicts. Try making sure you’ve installed it correctly: https://gravitywiz.com/documentation/snippet-troubleshooting/

  8. Eric says

    March 4, 2019 at 6:29 pm

    Hey, How would this work on a dropdown list? I have different inventory depending on the choice taken from the dropdown.

    Thanks for the reply

    Reply
    • David Smith says

      March 5, 2019 at 8:59 am

      Hi Eric, this would be a better fit for Limit Choices: https://gravitywiz.com/documentation/gravity-forms-limit-choices/

  9. Nick says

    March 3, 2019 at 11:33 pm

    Hello, noticed a number of updates in the change-log. Wondering if there is any documentation or explanation on how to use these additional features. Particularly field_group parameter “so that days with exhausted inventory or not available.” Looking at using a single form for booking an event which occurs on multiple separate days. Was wanting to get SUM based on a certain date field in the form entries. Thank you for this great plugin.

    Reply
    • David Smith says

      March 4, 2019 at 7:55 am

      Hi Nick, the field_group should be passed as an array of additional field IDs that are treated as a group to create a unique offering. For example, if you’re using a Number field to track inventory, you could group a Date field with that Number field via the field_group parameter.

      'field_group' => array( 2 ),

      …where 2 is the ID of your Date field.

  10. Max says

    February 13, 2019 at 2:37 pm

    Hi. I tried to use the code and: 1. I pasted code from”download code to function.php then 2 I’ve pasted code drom snippet to function php 4 I created a form with quantity field (separate with drop down list, id 9 (without .3) ) 5. I created pages – one with form, second with “remaining” and “sum” shortcodes. 6. I edited snippet in functions.php and in shortcodes. uff. ;-)

    so – I can choose quantity, it is correct in forms , but shortcodes still shows 50 (maximum) of remaining and 0 of purchased.

    I thought it was a mistake to input snippet (configuration) into “function php” so i used “code snippet” plugin and after pasting and activation – white screen of death… I was using demo installation at “gravitydemo.com”

    So where should I paste a snippet code? into functions php?

    Reply
    • Max says

      February 13, 2019 at 7:31 pm

      OK. I see “code” and a “snippet code” “Code” should be pasted to “functions.php” and where should be pladed “snippet code”? I placed both in “functions .php” but it does not work.

      Shotrcode returns remaining 50 and sum 0. Id was correct (quantity) tested both – single 8.3 and standalone quantity “9” No changes when I “purchased” items – but in database I saw, that there was changes. Looks like snippet did not take data from database.

    • David Smith says

      February 13, 2019 at 7:52 pm

      Hi Max, if you’re having trouble installing the snippet, this is a good place to start: https://gravitywiz.com/documentation/snippet-troubleshooting/

    • max says

      February 14, 2019 at 4:10 pm

      Ok just simple question – should it work properly with BOTH codes in “function.php” ? For me it is working “partially”

      Or is it necessary to place “main code” in function and a snippet in other place.

    • David Smith says

      February 14, 2019 at 5:03 pm

      They go together.

  11. Nate says

    January 24, 2019 at 1:03 pm

    I’m using this and it seems to be working.

    How do I turn off a field when it’s full?

    Here’s the form: https://salifeline.org/sa-lifeline-womens-retreat-2019/

    Thanks

    Reply
    • David Smith says

      January 25, 2019 at 4:00 am

      Hi Nate, not sure I understand. The snippet should automatically disable the field when the limit is reached?

    • Nate says

      January 25, 2019 at 5:59 pm

      I thought so too David.

      Could it be because we are using Radio Button fields that then trigger a Product field based on conditional logic?

      Let me know what additional information I can provide.

      Thanks.

    • David Smith says

      January 28, 2019 at 12:34 pm

      If you’re a Gravity Perks customer, we’ll be happy to provide support via the support form.

  12. Kuldeep says

    January 21, 2019 at 7:48 am

    I have used your inventory code and that is working perfect but i need to know one thing if the room is occupied then free for use then how can we re available that space for use ??

    Thanks K!

    Reply
    • David Smith says

      January 21, 2019 at 8:31 am

      Hi Kuldeep, you will need to delete the entry that reserved the inventory or edit it so that it no longer reserves the inventory.

    • Kuldeep says

      January 21, 2019 at 8:37 am

      You means from the database ? or the code i have added http://prntscr.com/ma2x1g

    • David Smith says

      January 22, 2019 at 1:19 am

      I mean in the Gravity Forms admin. When you submit the form it creates an entry. Each entry stores the data that was submitted, including the requested inventory for a specific product/field.

    • Kuldeep says

      January 22, 2019 at 2:41 am

      ok thanks!

  13. Steve says

    January 7, 2019 at 11:29 pm

    This snippet works great, thank you very much!

    Question: who gets the email when

    ‘enable_notifications’ => true

    , and is there any way to configure which email it goes to? I initially looked in Notifications for an Event but didn’t see an inventory related event in the drop down. Cheers!

    Reply
    • Steve says

      January 10, 2019 at 8:03 pm

      Probably related, I have this in my debug log. Works great otherwise! Thanks

      PHP Notice: GW_Inventory::$enable_notifications was called incorrectly. Inventory notifications require the ‘GW_Notification_Event’ class. Please see Debugging in WordPress for more information. (This message was added in version 1.0.) in /home/username/public_html/wp-includes/functions.php on line 4161

    • David Smith says

      January 13, 2019 at 8:00 am

      Hi Steve, you’ll need to also install this snippet to use the notifications feature.

      https://gist.github.com/spivurno/bd9d8e118f45999fb08c

  14. Jorrit van Harten says

    January 6, 2019 at 6:57 pm

    Hi David,

    First of all: thanks for creating such usefull codes…!

    Regarding the question I had the shortcode: Shortcode: Display Number of Entries Left, you directed me to this shortcode. I installed it on my website and it first looked to be working fine.

    However in my case the following isn’t working. I have multiple forms for workshops (4). These forms all have a specific form id off course. Every workshop has it’s own limit of allowed participants (mostly 12). For this form I use the productfield with list of choices to add the amount of participants.

    When I use this snippet: new GW_Inventory( array( ‘form_id’ => 1, ‘field_id’ => 9, ‘stock_qty’ => 12, ‘out_of_stock_message’ => ‘Sorry, er is helaas geen plaats meer voor deze workshop. Zoek een andere workshop uit of neem contact met ons op voor de mogelijkheden’, ‘not_enough_stock_message’ => ‘Je wilde %1$s deelnemers aanmelden voor onze workshop. Er zijn maar %2$s plaatsen over.’, ‘approved_payments_only’ => false, ‘hide_form’ => false, ‘enable_notifications’ => true ) );

    it workes great for one workshop. But how do I make this work for all workshops? I tried creating multiple of above snippets and also took a look at your page https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/, but I can’t seem to figger it out.

    Can you please assist?

    Regards,

    Jorrit

    Reply
    • Jorrit van Harten says

      January 6, 2019 at 7:00 pm

      I’m using the shortcode [gravityforms action="remaining" id="1" input_id="2.3" limit="50"]

  15. Jennifer says

    December 20, 2018 at 2:25 pm

    Just wondering, if I have multiple product fields on the same page that all start with the same stock quantity, can I pass on multiple field ids at once in the initialization script?

    Thanks again!

    Reply
    • David Smith says

      December 20, 2018 at 3:46 pm

      This snippet must be configured per field. You would just duplicate the configuration bit at the bottom and replace the field ID.

  16. Jennifer says

    December 20, 2018 at 2:19 pm

    Hi, Is there a way to use this snipped if my product field is set to the Radio Buttons field type. We have a form where users can select from up to 8 choices and I’d like to track inventory for each choice (ideally be able to set a different stock quantity for each choice).

    Is this possible? I’d be happy to pay for your time if the snippet would need to be modified. Thanks!

    Reply
    • David Smith says

      December 20, 2018 at 3:45 pm

      Choice-specific inventory is better handled by our Limit Choices plugin:

      https://gravitywiz.com/documentation/gravity-forms-limit-choices/

    • Jennifer Feurer says

      December 21, 2018 at 9:39 pm

      Thanks for pointing out the Limit Choices perk — it’s exactly what I need!

  17. Kitty Bakker says

    November 21, 2018 at 3:52 am

    I used the plugin, but it does no longer work well, so I am interested in using your way. But Is there a way I can set this up for my client, so he will be able to adjust the quantity limits? I cannot give him access to the functions.php.

    $stock_qty the number of this item you have available.

    Thanx!

    Reply
    • David Smith says

      December 1, 2018 at 9:59 am

      Hi Kitty, the “stock_qty” does not need to be a static value. You can set it as a callable function which would allow you to retrieve the stock dynamically. Here’s an example where the quantity is fetched from a “stock” custom field on whatever post/page the form is embedded.

      http://snippi.com/s/sr3ccf4

  18. Viv says

    November 16, 2018 at 12:14 pm

    Is there a way to use this globally for all forms instead of limiting it to just one?

    Reply
    • David Smith says

      November 16, 2018 at 12:29 pm

      Hi Viv, since the inventory is for a specific field, it must also be for a specific form. If you’re looking to limit submissions across all forms (rather than inventory), check out our Gravity Forms Limit Submission plugin.

  19. Peli says

    November 12, 2018 at 10:00 am

    Is there a way to display the remaining inventory quantity? Something like — There are 50 spots remaining —

    Reply
    • David Smith says

      November 12, 2018 at 4:55 pm

      Hi Peli, I’ve updated the article above with instructions on the available shortcodes with this snippet. You’ll want the “remaining” shortcode.

  20. Marino says

    October 12, 2018 at 9:20 am

    Hello,

    I was wondering if there was an easy option to send an email when the max quantity has been reached instead of displaying the “out_of_stock_message” That email just has to have that same message saying the max. number has been reached but the person who is using the form should be able to submit.

    Thanks for your replay.

    Reply
    • David Smith says

      October 12, 2018 at 9:56 am

      Hi Marino, you can enable notifications via the “enable_notifications” parameter. If “hide_form” is set to false, the form will not be hidden.

« Older Comments
Newer 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 (63)
  • News (21)
  • Plugins (14)
  • Releases (7)
  • Resource (3)
  • Snippets (58)
  • Tutorials (57)
  • Updates (104)

Recent Posts

  • How to Update Posts with Gravity Forms
  • Gravity Wiz Weekly #104
  • The Complete Guide to Using Gravity Forms With Zapier
  • Gravity Wiz Weekly #103
  • Show Active Forms by Default on Form List

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