Set Inventory Limit by Custom Field

This filter sets the inventory of an Advanced resource dynamically based on the inventory custom field of the post/page on which the form is embedded.

Code

Filename: gpi-set-inventory-limit-by-custom-field.php

<?php
/**
 * Gravity Perks // Inventory // Set Inventory Limit by Custom Field
 * https://gravitywiz.com/documentation/gravity-forms-inventory/
 *
 * This filter sets the inventory of an Advanced resource dynamically based on the
 * inventory custom field of the post/page on which the form is embedded.
 */
add_filter( 'gpi_inventory_limit_advanced', function() {
	// Update 'inventory' to the custom field meta key.
	return get_post_meta( get_queried_object_id(), 'inventory', true );
} );

Comments

  1. Kenny Waddell
    Kenny Waddell June 2, 2024 at 9:03 am

    Can you expand on how this works please, I’ve added the snippet but nothing seems to be happening. Do you need to select Inventory “Advanced” and add a scope ? or do you leave all unset and this just works – sorry I’m confused.

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff June 3, 2024 at 10:03 am

      Hi Kenny,

      The way it works is that you’ll need to update ‘inventory’ as commented in the code to match your meta key of the custom field with the inventory limit value. Then, you’ll need to set your Inventory type to Advanced and set up a Resource. A scope is not needed here. This snippet sets the inventory of an Advanced resource dynamically based on the value of the custom field.

      I hope this helps. However, if it’s still not working for you, send us a message via our support form so we can look into this on your end.

      Best,

Leave a Reply

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

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

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