May 3, 2023: Added support for tracking "inventory" on currency-formatted Number fields.
November 11, 2012: Fixed issue where database prefix was not applied via $wpdb-<prefix.
Stop! There's a better way.
This snippet is available as a plugin with Gravity Perks, a suite of over 47 premium Gravity Forms plugins!
Overview
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.
Using the Snippet
Prerequisites
Confirm that you have Gravity Forms installed and activated and that you’ve installed the snippet.
Add a Product Field
With the snippet installed, add a Product field to your form. Note the Product field’s ID.
Update Snippet with Parameters
Update the form_id
and field_id
in the snippet to match your Form ID and Field ID. Depending on the way your form is set up, you’ll need to format the Field IDs differently:
- If your form uses a separate Quantity field for the Product’s quantity, set the
field_id
to the Quantity field’s ID. - If your Product uses the built-in quantity sub-field, set the field ID to your Product ID and add
.3
to the end.
new GW_Inventory( array(
'form_id' => 123,
'field_id' => 4.3,
'stock_qty' => 20,
) );
Custom Stock Level Messages
Better Inventory includes default “out of stock” and “not enough stock” messages. These messages can be customized using the out_of_stock_message
and not_enough_stock_message
parameters.
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.',
) );
Out of Stock
The out_of_stock_message
is displayed when the product limit has been reached. You can get fancy with this and add HTML. Be sure to escape any double quotes with a backslash. Here’s an advanced example:
'out_of_stock_message' = '<div style="border: 1px solid #e6db55; background-color: #FFFFE0; padding: 10px;">Sorry, this show is sold out.</div>'
Not Enough Stock
The not_enough_stock_message
is displayed on the field if the limit has not been reached, but the user’s requested quantity would exceed the product limit.
Parameters
form_id (integer) (required)
The ID of the form you are working with.
field_id (integer|float) (required)
The ID of your product or quantity field. If using a Single Product field, specify the quantity input ID, which will always be
{field ID}.3
. If using a separate Quantity field, simply specify the field ID.stock_qty (integer) (required)
The number of this item you have available.
out_of_stock_message (string) (optional)
The message which should be displayed to users when the product limit has been reached.
not_enough_stock_message (string) (optional)
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.
approved_payments_only (boolean) (optional)
Indicate whether all submissions for this field should be counted against the limit or only submissions with an approved payment.
hide_form (boolean) (optional)
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 (boolean) (optional)
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"]
Taking It Further
Approved Payments
By default, all submissions are counted against the stock quantity. Use the approved_payments_only
parameter to indicate that only submissions with an approved payment should be counted. 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.
new GW_Inventory( array(
'form_id' => 123,
'field_id' => 4.3,
'stock_qty' => 20,
'approved_payments_only' => true,
) );
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 47+ essential add-ons for Gravity Forms with support you can count on.
I am getting a WP Notice when WP Debug mode is enabled. This is it:
Notice: Function 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 /wordpress/wp-includes/functions.php on line 5831
Would you please be so kind as to look into it? Thank you.
Hi Laz,
We’ve already followed up via email to confirm if the issue is related to GP Inventory or the Better Inventory Snippet.
Best,
Can I use this widget to log an event? I want to show a message if records are collected according to the capacity of an event.
There is a post_type named :Events For ::Event, register limit field (acf field)
:Single “Event Register” form ::Field: Post_id ::Field: Post_title
The event looks like this:
Title: Yoga Event Capacity: 20
The list of form entries looks like this:
Event_ID: 136 Event_Title: Yoga Event_Guest: 1
Hi Ayhan,
I am not sure this snippet would work for what you’re trying to do. You can get in touch with us via our support form if you have an Active Gravity Perks license so we can request more information on your use case and assist you further? However, if you don’t have a license, you can send submit our presales form with more information on your use case. Maybe one of our Perks can do what you want.
Best,
Hi, can use for multiple form ?
Hi Mustaqim,
You can! Multiple instances of the
GW_Inventory class
can be added to apply it to multiple forms. Read more about that here.Great, very well documented solution. I get it working on the first try.
Two questions: Is it possible to set a different limit for another product field?
And: Is it possible to get the amount of the remaining quantity inside the product field? At the moment I use the shortcode in a separate html field, but it would be better to show the value directly in the product field.
Hi Thomas,
You can insert multiple instances of the
GW_Inventory
class to apply it to multiple fields on the same form. More info here.Regarding displaying the quantity inside the field, that isn’t currently supported in the snippet. That said, we are cooking up a Perk that will include that feature. Keep an eye on our Weeklies!
Got this to work. I would like to use for a cafe who has a daily schedule with limited attendees for each day. This would mean we would need to have a separate form for each day. If there any thing you could do or suggest.
May Thanks
Hi Peter,
This snippet currently doesn’t support setting the number of stock available per day, so you will have to create a new form for each day, which will be a lot of work. A workaround solution will be to use our GP Limit Choices Perk to set a limit on a Product field with a single choice and then use our Daily Limit snippet to reset the limit left every day.
Best,
Hi Dario,
I’ve now set the Product as Single Product – https://eventsbuildertrendcom.kinsta.cloud/register/
But still it will not work. I’ve set the Stock Qty as 8, so in theory when you select 9 attendees it should give the sold out message? – http://snippi.com/s/iz1jn5n
Hi Jon,
If the stock is set to 10, and in the Quantity field the user set 12. This message should output: You ordered 12 tickets. There are only 10 tickets left. We’ve test locally and it seems to be working as expected. If you have a Gravity Perks license, you can get in touch with us via our support form with your account email, so we can help you troubleshoot.
Cheers,
hello, I am trying to implement this snippet, for my registration form – http://eventsbuildertrendcom.kinsta.cloud/register/
I have a hidden product with a price of $999 and then a separate quantity field (How many attendees? 1-10) selecting will update the Registration cost correctly. So to test i am trying to set a registration limit of 5 total, but it does not show the we are full message?
http://snippi.com/s/iz1jn5n
I’ve checked and the ID’s are correct?
Hi Jon,
Currently, the snippet doesn’t seem to support Products that are set to hidden but this feature request is something that we will consider in the future.
Cheers,