|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Perks // Inventory // Dynamic Scopes for Combo Inventories |
| 4 | + * https://gravitywiz.com/documentation/gravity-forms-inventory/ |
| 5 | + * |
| 6 | + * Imagine a form with two Drop Down fields with available time slots. One Time Slot field is for a Standard package. |
| 7 | + * This field has inventory enabled and is scoped to a Date field and a Room field (a Drop Down field with available |
| 8 | + * rooms). If a Standard package is selected, a Room must also be selected. |
| 9 | + * |
| 10 | + * The other Time Slot field is for an Ultimate package. This field is configured identically to the Standard package; |
| 11 | + * however, Ultimate packages should claim inventory for both rooms for a given Date and Time Slot. When an Ultimate |
| 12 | + * package is selected, the Room field is hidden via conditional logic. |
| 13 | + * |
| 14 | + * The goal of this snippet is to ensure that the Ultimate field's claimed inventory applies to the Standard field's |
| 15 | + * inventory such that if the 2pm time slot is claimed for Room A (or Room B) in the Standard field, the 2pm time slot |
| 16 | + * in the Ultimate field is also claimed. Similarly, if the 2pm time slot is claimed in the Ultimate field, the 2pm time |
| 17 | + * slot should be claimed for both Room A and Room B in the Standard field (since the Ultimate package reserves both). |
| 18 | + * |
| 19 | + * This snippet achieves this by altering the query to remove the second scope (in this case, the Room field). The rules |
| 20 | + * for dictating when the Room scope is removed are explained in comments in the snippet below. |
| 21 | + * |
| 22 | + * Instructions: |
| 23 | + * |
| 24 | + * 1. Install the snippet. |
| 25 | + * https://gravitywiz.com/documentation/managing-snippets/#where-do-i-put-snippets |
| 26 | + * |
| 27 | + * 2. Update the inline variables with your form and field IDs. |
| 28 | + */ |
| 29 | +add_filter( 'gpi_query', function( $query, $field ) { |
| 30 | + |
| 31 | + $form_id = 1133; |
| 32 | + $standard_field_id = 4; |
| 33 | + $super_field_id = 6; |
| 34 | + $ultimate_field_id = 9; |
| 35 | + |
| 36 | + /** |
| 37 | + * We're querying for the Ultimate field's (an inventory-enabled Radio Button field with time slots) inventory. Lets |
| 38 | + * ensure that the Standard and Super fields' claimed inventory is included in the Ultimate field's. We do this below |
| 39 | + * by altering the query to remove the second scope (which is a Drop Down field with available rooms). |
| 40 | + */ |
| 41 | + if ( $field->id === $ultimate_field_id ) { |
| 42 | + $fields_to_alter = array( $standard_field_id, $super_field_id, $ultimate_field_id ); |
| 43 | + } |
| 44 | + /** |
| 45 | + * We're querying for the Standard or Super field's (inventory-enabled Drop Down fields with available rooms) inventory. |
| 46 | + * Let's ensure that the Ultimate field's claimed inventory is included in the Standard and Super fields'. |
| 47 | + */ |
| 48 | + else if ( in_array( $field->id, array( $standard_field_id, $super_field_id ) ) ) { |
| 49 | + $fields_to_alter = array( $ultimate_field_id ); |
| 50 | + } else { |
| 51 | + return $query; |
| 52 | + } |
| 53 | + |
| 54 | + foreach( $fields_to_alter as $current_field_id ) { |
| 55 | + $query['where'] = preg_replace( |
| 56 | + "/\(e.form_id = {$form_id} AND em.form_id = {$form_id} AND em.meta_key = '{$current_field_id}'\) AND ([a-z0-9_]+)\.meta_value = '(.*?)' AND [a-z0-9_]+\.meta_value = '(.*?)'/", |
| 57 | + "(e.form_id = {$form_id} AND em.form_id = {$form_id} AND em.meta_key = '{$current_field_id}') AND \${1}.meta_value = '\${2}'", |
| 58 | + $query['where'] |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + return $query; |
| 63 | +}, 10, 2 ); |
0 commit comments