Skip to content

Commit 87394fc

Browse files
authored
gpi-exhausted-inventory-threshold.php: Added new snippet.
1 parent d1b5215 commit 87394fc

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Inventory // Exhausted Inventory Threshold
4+
* https://gravitywiz.com/documentation/gravity-forms-inventory/
5+
*
6+
* Mark field as out of stock when a given number of choices with exhausted inventory is reached.
7+
* For example, if you had 5 choices and wanted to disable the field after 3 choices' inventory
8+
* was exhausted, this snippet would do the trick.
9+
*
10+
* Instructions
11+
*
12+
* 1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
13+
* 2. Follow the inline instructions to configure the snippet for your form.
14+
*/
15+
// Update "123" to your form ID and "4" to your Inventory-enabled field ID.
16+
add_filter( 'gpi_is_in_stock_123_4', function( $is_in_stock, $field, $stock ) {
17+
$disabled_count = 0;
18+
foreach ( $field->choices as $choice ) {
19+
if ( rgar( $choice, 'isDisabled' ) ) {
20+
$disabled_count++;
21+
}
22+
}
23+
// Update "3" to the number of choices with exhausted inventory required to mark the field as out of stock.
24+
if ( $disabled_count >= 3 ) {
25+
$is_in_stock = false;
26+
}
27+
return $is_in_stock;
28+
}, 10, 3 );

0 commit comments

Comments
 (0)