Packaged Products

Experimental Snippet 🧪

Add dependencies between packaged products and products that are individually in the product. As packages are bought, the individual items available inventory will also decrease by that much. If the available inventory of an individual product is below the inventory of a package, it should use the lowest inventory amount amongst the packaged fields.

Known Limitations:

  • The package field and fields in the package must be using the Advanced Inventory type and have their own resources.
  • All fields need to be on the same form.

Instructions

Code

Filename: gpi-packaged-products.php

<?php
/**
 * Gravity Perks // Inventory // Packaged Products
 * https://gravitywiz.com/documentation/gravity-forms-inventory/
 *
 * Experimental Snippet 🧪
 *
 * Add dependencies between packaged products and products that are individually in the product. As packages are
 * bought, the individual items available inventory will also decrease by that much. If the available inventory of an
 * individual product is below the inventory of a package, it should use the lowest inventory amount amongst the packaged
 * fields.
 *
 * Known Limitations:
 *    - The package field and fields in the package must be using the Advanced Inventory type and have their own resources.
 *    - All fields need to be on the same form.
 *
 * Instructions:
 *    - Install using instructions here: https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 *    - Update the configuration at the bottom of the snippet accordingly.
 */
class GP_Inventory_Packaged_Products {
	/** @var array */
	private $_args;

	/** @var int */
	public $form_id;

	/** @var GF_Field */
	public $package_field;

	/** @var int[] */
	public $field_ids_in_package = array();

	public function __construct( $args = array() ) {
		$this->_args = wp_parse_args( $args, array(
			'form_id'              => null,
			'package_field_id'     => null,
			'field_ids_in_package' => array(),
		) );

		$this->form_id              = $this->_args['form_id'];
		$this->package_field        = GFAPI::get_field( $this->form_id, $this->_args['package_field_id'] );
		$this->field_ids_in_package = $this->_args['field_ids_in_package'];

		add_action( 'init', array( $this, 'init' ) );
	}

	public function init() {
		add_filter( 'gpi_claimed_inventory_' . $this->form_id, array(
			$this,
			'package_field_claimed_inventory',
		), 10, 2 );

		add_filter( 'gpi_claimed_inventory_' . $this->form_id, array(
			$this,
			'add_claimed_package_inventory_to_individual_products',
		), 10, 2 );

		add_filter( 'gpi_requested_quantity_' . $this->form_id, array(
			$this,
			'add_requested_packages_to_individual_products',
		), 10, 2 );
	}

	/**
	 * Add claimed inventory of packaged items to packages if their inventory is below packages.
	 */
	public function package_field_claimed_inventory( $package_claimed_inventory, $field ) {
		if ( $field->id !== $this->package_field->id ) {
			return $package_claimed_inventory;
		}

		remove_filter( 'gpi_claimed_inventory_' . $this->form_id, array(
			$this,
			'add_claimed_package_inventory_to_individual_products',
		) );

		$inventory_limit                 = gp_inventory_type_advanced()->get_stock_quantity( $this->package_field );
		$package_available_inventory     = $inventory_limit - $package_claimed_inventory;
		$packaged_item_available_amounts = array();

		foreach ( $this->field_ids_in_package as $field_id_in_package ) {
			$field_in_package                  = GFAPI::get_field( $this->form_id, $field_id_in_package );
			$packaged_item_available_amount    = gp_inventory_type_advanced()->get_available_stock( $field_in_package ) - $package_claimed_inventory;
			$packaged_item_available_amounts[] = $packaged_item_available_amount;
		}

		$lowest_package_item_available_amount = min( $packaged_item_available_amounts );

		if ( $lowest_package_item_available_amount < $package_available_inventory ) {
			$difference                = $package_available_inventory - $lowest_package_item_available_amount;
			$package_claimed_inventory = $package_claimed_inventory + $difference;
		}

		add_filter( 'gpi_claimed_inventory_' . $this->form_id, array(
			$this,
			'add_claimed_package_inventory_to_individual_products',
		), 10, 2 );

		return $package_claimed_inventory;
	}

	/**
	 * Add claimed inventory of packages to packaged items.
	 */
	public function add_claimed_package_inventory_to_individual_products( $claimed_inventory, $field ) {
		if ( ! in_array( $field->id, $this->field_ids_in_package ) ) {
			return $claimed_inventory;
		}

		remove_filter( 'gpi_claimed_inventory_' . $this->form_id, array(
			$this,
			'package_field_claimed_inventory',
		) );

		$claimed_packages_inventory = gp_inventory_type_advanced()->get_claimed_inventory( $this->package_field );

		add_filter( 'gpi_claimed_inventory_' . $this->form_id, array(
			$this,
			'package_field_claimed_inventory',
		), 10, 2 );

		return (int) $claimed_inventory + (int) $claimed_packages_inventory;
	}

	/**
	 * Add requested quantity of packages to packaged items.
	 */
	public function add_requested_packages_to_individual_products( $requested_quantity, $field ) {
		if ( ! in_array( $field->id, $this->field_ids_in_package ) ) {
			return $requested_quantity;
		}

		$requested_packages_qty = rgpost( 'input_' . $this->package_field->id . '_3' );

		return (int) $requested_quantity + (int) $requested_packages_qty;
	}

}

/**
 * Configuration
 */
new GP_Inventory_Packaged_Products( array(
	'form_id'              => 2,
	'package_field_id'     => 4,
	'field_ids_in_package' => array( 1, 2 ),
) );

Comments

    1. J Yeager
      J Yeager Staff June 9, 2025 at 7:21 pm

      Hey Mary,

      Oh, awesome! That sounds like it would do the trick. We’re glad to hear it 😃

  1. Mary Makowsky
    Mary Makowsky June 5, 2025 at 3:30 pm

    Hello,

    I have a situation where we are selling tickets to an event and also selling tables. So, if someone buys a table, it should remove 8 tickets from the resource inventory. How would I specify that in this? Or is this even the correct snippet for this purpose?

    Thank you!

    Reply
    1. J Yeager
      J Yeager Staff June 5, 2025 at 6:24 pm

      Hey Mary,

      For your purpose, rather than going for this snippet and packaged products, it sounds like the standard advanced inventory feature would be a better approach. For example, if you had a shared resource called “slots”, and two products (tickets and tables) you would want to set a default quantity of 1 slot for the “ticket” product, and a quantity of 8 slots for your “table” product. Since both of those products would pull from the same “slots” resource, the inventory would decrement appropriately without any bundling involved.

      Hope that follows, and if you have a form already in progress, we’d be happy to take a closer look at your setup via support!

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.