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

Set up simple, one-off inventory limits per field.

Last updated August 23, 2021 | Written by David Smith 556 Comments

  • 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 42 premium Gravity Forms plugins!

  • View the Plugin
  • Buy Gravity Perks
View CodeDownload Code

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.

  1. Overview
  2. Using the Snippet
    1. Prerequisites
    2. Add a Product Field
    3. Update Snippet with Parameters
      1. Custom Stock Level Messages
        1. Out of Stock
        2. Not Enough Stock
  3. Parameters
  4. Shortcodes
  5. Taking It Further
    1. Approved Payments

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

enable_notifications requires our Notification Events snippet to be installed on your site.

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 41+ essential add-ons for Gravity Forms with support you can count on.

  • View All Perks
  • Buy Gravity Perks

Filed Under: Snippets

Comments

  1. ayhan says

    June 4, 2022 at 10:13 am

    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

    Reply
    • Samuel Bassah says

      June 6, 2022 at 6:29 am

      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,

  2. Mustaqim says

    October 29, 2021 at 10:18 am

    Hi, can use for multiple form ?

    Reply
    • Scott Buchmann says

      October 29, 2021 at 10:45 am

      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.

  3. Thomas Fiedler says

    August 13, 2021 at 4:35 am

    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.

    Reply
    • Scott Buchmann says

      August 13, 2021 at 11:49 am

      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!

  4. Peter Condon says

    July 30, 2021 at 7:37 am

    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

    Reply
    • Samuel Bassah says

      July 30, 2021 at 7:48 am

      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,

  5. Jon says

    July 16, 2021 at 9:16 am

    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

    Reply
    • Dario Space says

      July 16, 2021 at 10:09 am

      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,

  6. Jon says

    July 14, 2021 at 2:29 pm

    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?

    Reply
    • Dario Space says

      July 14, 2021 at 7:24 pm

      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,

« Older Comments

Leave a Reply Cancel reply

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

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.

Recent Posts

  • Spotlight: Gravity Forms Media Library 🤝 Advanced Custom Fields
  • How ANM Powers Internal Finances with GP Unique ID
  • New Release: Gravity Forms QR Code Beta 1
  • How Farbie.at uses GP Inventory to manage over one thousand parties a year
  • Spotlight: Building a Loan Calculator with GP Advanced Calculations

Categories

  • How To (73)
  • News (25)
  • Plugins (19)
  • Releases (13)
  • Resource (3)
  • Snippets (50)
  • Spotlights (14)
  • Tutorials (57)
  • Uncategorized (5)
  • Updates (152)

Meta

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

Copyright © 2022 · Powered by WordPress · Gravity Wiz LLC

  • Support
  • Affiliates
  • About
  • Sitemap
  • Terms & Conditions of Use
  • Privacy Policy
  • Cookies Policy

Want to become a
Gravity Forms wizard?

Join 15,000 readers and be the first to know of legendary discounts, new releases, free snippets, and other secret sorcery!

Download Snippet

Better Inventory with Gravity Forms

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