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.
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 is12
, the quantity input ID will be12.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 totrue
.$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.
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.
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.
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.
Is there a way to add a out of stock css class to the field Id, for exemple deactivate the field?
Hi Benjamin, by default, the field will be “deactivated” and replaced by the out of stock message.
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 ) );
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.
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”
You would need to set the
approved_payments_only
parameter when you’re initializing theGW_Inventory
class.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
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.
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
Hi Tayler, this will help:
https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
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?
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.
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?
There are no known conflicts. Try making sure you’ve installed it correctly: https://gravitywiz.com/documentation/snippet-troubleshooting/
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
Hi Eric, this would be a better fit for Limit Choices: https://gravitywiz.com/documentation/gravity-forms-limit-choices/
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.
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 thefield_group
parameter.'field_group' => array( 2 ),
…where 2 is the ID of your Date field.
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?
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.
Hi Max, if you’re having trouble installing the snippet, this is a good place to start: https://gravitywiz.com/documentation/snippet-troubleshooting/
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.
They go together.
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
Hi Nate, not sure I understand. The snippet should automatically disable the field when the limit is reached?
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.
If you’re a Gravity Perks customer, we’ll be happy to provide support via the support form.
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!
Hi Kuldeep, you will need to delete the entry that reserved the inventory or edit it so that it no longer reserves the inventory.
You means from the database ? or the code i have added http://prntscr.com/ma2x1g
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.
ok thanks!
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!
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
Hi Steve, you’ll need to also install this snippet to use the notifications feature.
https://gist.github.com/spivurno/bd9d8e118f45999fb08c
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
I’m using the shortcode [gravityforms action="remaining" id="1" input_id="2.3" limit="50"]
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!
This snippet must be configured per field. You would just duplicate the configuration bit at the bottom and replace the field ID.
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!
Choice-specific inventory is better handled by our Limit Choices plugin:
https://gravitywiz.com/documentation/gravity-forms-limit-choices/
Thanks for pointing out the Limit Choices perk — it’s exactly what I need!
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!
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
Is there a way to use this globally for all forms instead of limiting it to just one?
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.
Is there a way to display the remaining inventory quantity? Something like — There are 50 spots remaining —
Hi Peli, I’ve updated the article above with instructions on the available shortcodes with this snippet. You’ll want the “remaining” shortcode.
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.
Hi Marino, you can enable notifications via the “enable_notifications” parameter. If “hide_form” is set to false, the form will not be hidden.