Limit by Field Value Match

Only count an entry towards the inventory limit if a specific field matches a specific value.

Code

Filename: gpi-limit-by-field-value-match.php

<?php
/**
 * Gravity Perks // Inventory // Limit by Field Value Match
 * https://gravitywiz.com/documentation/gravity-forms-inventory/
 *
 * Only count an entry towards the inventory limit if a specific field matches a specific value.
 */
// Update "123" to your form ID and "4" to your Inventory-enabled field ID.
add_filter( 'gpi_query_123_4', function ( $query ) {
	global $wpdb;

	// Update "5" to the ID of the field you would like to match and "xyz" to the value you would like to match.
	$query['where'] .= ' AND em_by_field.meta_key = "5" AND em_by_field.meta_value = "xyx" ';
	$query['join']  .= " INNER JOIN {$wpdb->prefix}gf_entry_meta em_by_field ON em_by_field.entry_id = em.entry_id ";

	return $query;
} );

Comments

  1. Ching
    Ching June 18, 2024 at 2:58 pm

    Hi! Thank you for the snippet — This snippet helps me manage the inventory at the event level for each ticket type on an event registration form that will be reused for different events throughout of the year. A quick question though: how do I use this hook in a loop so that I can apply the scope to 20 different ticket types on the form? Currently I do it for each product field like below, but I am trying to do that in 1 function if possible: add_filter( ‘gpi_query_9_78’, function ( $query ) { global $wpdb; $evt_code = get_field(‘evt_code’); $query[‘join’] .= ‘ INNER JOIN wp_gf_entry_meta em_by_field ON em_by_field.entry_id = em.entry_id ‘; $query[‘where’] .= ‘ AND em_by_field.meta_key = “34” AND em_by_field.meta_value = ‘.'”‘.$evt_code.'”‘; return $query; } );

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff June 19, 2024 at 5:21 am

      Hi Ching,

      Glad to know the snippet works for you. Your request will require some customization to the snippet, and as such we’ll need you to submit a ticket for this.

      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.