Waiting List for Exhausted Choices

Replace the default available inventory message with a waiting list message when a product’s (or choice’s) inventory is exhausted.

Choices on the waiting list will remain selectable and submittable. Non-choice product fields will remain available rather than being removed

Instructions

Code

Filename: gpi-waiting-list.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
 * Gravity Perks // Inventory // Waiting List for Exhausted Choices
 * https://gravitywiz.com/documentation/gravity-forms-inventory/
 *
 * Instruction Video: https://www.loom.com/share/7a5e57ec14404b9080c5e9b9878e2ecc
 *
 * Replace the default available inventory message with a waiting list message when a product's (or choice's) inventory
 * is exhausted.
 *
 * Choices on the waiting list will remain selectable and submittable. Non-choice product fields will remain available
 * rather than being removed
 */
class GPI_Waiting_List {

	private $_args = array();

	public $waitlist_message = '(waiting list)';

	public function __construct( $args = array() ) {

		// set our default arguments, parse against the provided arguments, and store for use throughout the class
		$this->_args = wp_parse_args( $args, array(
			'form_id'  => false,
			'field_id' => false,
		) );

		add_action( 'init', array( $this, 'add_hooks' ), 20 ); // Wait for GPI to be instantiated
	}

	public function add_hooks() {
		if ( ! function_exists( 'gp_inventory_type_advanced' ) || ! function_exists( 'gp_inventory_type_simple' ) ) {
			return;
		}

		add_filter( 'gform_entry_post_save', array( $this, 'add_entry_meta' ), 10, 2 );

		/*
		 * Choice-based hooks
		 */
		add_filter( 'gpi_disable_choices', '__return_false' );
		add_filter( 'gpi_remove_choices', '__return_false' );

		add_filter( 'gpi_pre_render_choice', array( $this, 'pre_render_choice' ), 10, 5 );

		// Add support for showing waiting list message in confirmations and notifications.
		add_filter( 'gform_pre_submission_filter', array( $this, 'add_waitlist_message_to_choices_on_submission' ), 10, 2 );

		add_filter( 'gform_entries_field_value', array( $this, 'entries_field_value_with_waitlist_message' ), 10, 4 );
		add_filter( 'gform_entry_field_value', array( $this, 'add_waitlist_message_to_entry_value' ), 10, 4 );

		/**
		 * Single products
		 */
		// Mark item as waitlisted during submission for other processes such as saving meta
		add_filter( 'gform_pre_submission_filter', array( $this, 'add_waiting_list_to_single_product' ) );

		// Prevent validation of inventory
		remove_filter( 'gform_validation', array( gp_inventory_type_advanced(), 'validation' ) );
		remove_filter( 'gform_validation', array( gp_inventory_type_simple(), 'validation' ) );

		// Remove locking out single products.
		add_filter( 'gform_field_input', array( $this, 'remove_gform_field_input_filters' ), 15, 2 );

		add_filter( 'gform_pre_render', array( $this, 'add_waiting_list_to_single_product' ) );

		// Allow negative stock to be used for conditional logic validation.
		add_filter( 'gpi_allow_negative_stock', array( $this, 'allow_negative_stock_for_conditional_logic' ), 10, 3 );
	}

	public function is_applicable_form( $form ) {
		$form_id = isset( $form['id'] ) ? $form['id'] : $form;

		return empty( $this->_args['form_id'] ) || $form_id == $this->_args['form_id'];
	}

	public function is_applicable_field( $field ) {
		$field_id = isset( $field['id'] ) ? $field['id'] : $field;

		return empty( $this->_args['field_id'] ) || $this->_args['field_id'] == $field_id;
	}

	public function remove_gform_field_input_filters( $return, $field ) {
		if ( $this->is_applicable_form( $field->formId ) && $this->is_applicable_field( $field ) ) {
			remove_filter( "gform_field_input_{$field->formId}_{$field->id}", array(
				gp_inventory_type_simple(),
				'hide_field',
			) );

			remove_filter( "gform_field_input_{$field->formId}_{$field->id}", array(
				gp_inventory_type_advanced(),
				'hide_field',
			) );
		}

		return $return;
	}

	public function entries_field_value_with_waitlist_message( $value, $form_id, $field_id, $entry ) {
		$form  = GFAPI::get_form( $form_id );
		$field = GFAPI::get_field( $form, $field_id );

		if ( $this->is_applicable_form( $form ) && $this->is_applicable_field( $field ) ) {
			$value = $this->add_waitlist_message_to_entry_value( $value, $field, $entry, $form );
		}

		return $value;
	}

	public function pre_render_choice( $choice, $exceeded_limit, $field, $form, $count ) {
		if ( $this->is_applicable_form( $form ) && $this->is_applicable_field( $field ) ) {
			$limit         = (int) rgar( $choice, 'inventory_limit' );
			$how_many_left = max( $limit - $count, 0 );

			if ( $how_many_left <= 0 ) {
				$choice                 = $this->apply_waitlist_message_to_choice( $choice, $field, $form, $how_many_left );
				$choice['isWaitlisted'] = true;
			}
		}
		return $choice;
	}

	public function apply_waitlist_message_to_choice( $choice, $field, $form, $how_many_left = false ) {
		if ( $this->is_applicable_form( $form ) && $this->is_applicable_field( $field ) ) {
			$message         = $this->waitlist_message;
			$default_message = gp_inventory_type_choices()->replace_choice_available_inventory_merge_tags( gp_inventory_type_choices()->get_inventory_available_message( $field ), $field, $form, $choice, $how_many_left );
			if ( strpos( $choice['text'], $default_message ) === false ) {
				$choice['text'] .= ' ' . $message;
			} else {
				$choice['text'] = str_replace( $default_message, $message, $choice['text'] );
			}
		}

		return $choice;
	}

	public function add_waitlist_message_to_entry_value( $value, $field, $entry, $form ) {
		if ( ! $this->is_applicable_form( $form ) || ! $this->is_applicable_field( $field ) ) {
			return $value;
		}

		if ( gp_inventory_type_choices()->is_applicable_field( $field ) ) {
			foreach ( $field->choices as $choice ) {
				if ( $choice['text'] != $value ) {
					continue;
				}

				$is_waitlisted = gform_get_meta( $entry['id'], sprintf( 'gpi_is_waitlisted_%d_%s', $field->id, sanitize_title( $choice['value'] ) ) );

				if ( $is_waitlisted ) {
					$choice = $this->apply_waitlist_message_to_choice( $choice, $field, $form );
					$value  = $choice['text'];
				}
			}
		}

		if ( gp_inventory_type_simple()->is_applicable_field( $field ) || gp_inventory_type_advanced()->is_applicable_field( $field ) ) {
			$is_waitlisted = gform_get_meta( $entry['id'], sprintf( 'gpi_is_waitlisted_%d', $field->id ) );

			if ( $is_waitlisted ) {
				$value .= ' ' . $this->waitlist_message;
			}
		}

		return $value;
	}

	public function add_entry_meta( $entry, $form ) {
		if ( ! $this->is_applicable_form( $form ) ) {
			return $entry;
		}

		foreach ( $form['fields'] as $field ) {
			if ( ! $this->is_applicable_field( $field ) ) {
				continue;
			}

			// Checks all inventory types
			if ( ! gp_inventory_conditional_logic()->is_applicable_field( $field ) ) {
				continue;
			}

			if ( gp_inventory_type_choices()->is_applicable_field( $field ) ) {
				foreach ( $field->choices as $choice ) {
					if ( rgar( $choice, 'isWaitlisted' ) ) {
						gform_add_meta( $entry['id'], sprintf( 'gpi_is_waitlisted_%d_%s', $field->id, sanitize_title( $choice['value'] ) ), true );
					}
				}
			}

			if ( gp_inventory_type_simple()->is_applicable_field( $field ) || gp_inventory_type_advanced()->is_applicable_field( $field ) ) {
				if ( rgar( $field, 'isWaitlisted' ) ) {
					gform_add_meta( $entry['id'], sprintf( 'gpi_is_waitlisted_%d', $field->id ), true );
				}
			}
		}

		return $entry;
	}

	public function add_waitlist_message_to_choices_on_submission( $form ) {
		if ( ! $this->is_applicable_form( $form ) ) {
			return $form;
		}

		foreach ( $form['fields'] as &$field ) {
			if ( ! $this->is_applicable_field( $field ) ) {
				continue;
			}

			// Checks all inventory types
			if ( ! gp_inventory_conditional_logic()->is_applicable_field( $field ) ) {
				continue;
			}

			if ( gp_inventory_type_choices()->is_applicable_field( $field ) ) {
				$choice_counts = gp_inventory_type_choices()->get_choice_counts( $form['id'], $field );
				$choices       = $field['choices'];

				foreach ( $choices as &$choice ) {
					$value        = $field->sanitize_entry_value( $choice['value'], $form['id'] );
					$choice_count = intval( rgar( $choice_counts, $value ) );
					$choice       = gf_apply_filters( array(
						'gpi_pre_render_choice',
						$form['id'],
						$field->id,
					), $choice, null, $field, $form, $choice_count );
				}

				$field['choices'] = $choices;
			}
		}

		return $form;
	}

	public function add_waiting_list_to_single_product( $form ) {
		if ( ! $this->is_applicable_form( $form ) ) {
			return $form;
		}

		foreach ( $form['fields'] as &$field ) {
			if ( ! $this->is_applicable_field( $field ) ) {
				continue;
			}

			if ( ! gp_inventory_type_simple()->is_applicable_field( $field ) && ! gp_inventory_type_advanced()->is_applicable_field( $field ) ) {
				continue;
			}

			$gpi_instance = gp_inventory_type_simple()::$type === rgar( $field, 'gpiInventory' ) ? gp_inventory_type_simple() : gp_inventory_type_advanced();

			$available = (int) $gpi_instance->get_available_stock( $field );

			if ( $available <= 0 ) {
				$message             = $this->waitlist_message;
				$field->description  = '<div class="gpi-available-inventory-message" style="padding-bottom: 13px;">' . $message . '</div>' . $field->description;
				$field->isWaitlisted = true;
			}
		}

		return $form;
	}

	public function allow_negative_stock_for_conditional_logic( $allow_negative_stock, $target_field, $form ) {
		if ( ! $this->is_applicable_form( $form ) ) {
			return $allow_negative_stock;
		}

		return true;
	}

}

new GPI_Waiting_List();

/**
 * @todo Add support for searching for entries that contain items from any waitlist or a product/choice-specific waitlist.
 */
//add_filter( 'gform_field_filters', function( $field_filters, $form ) {
//	$field_filters[] = array(
//		'text' => 'Waiting List',
//		'operators' => array( 'is', 'isnot' ),
//		'key' => 'gpi_waiting_list',
//		'preventMultiple' => false,
//		'values' => array(
//			array(
//				'text' => 'Any Waitlist',
//				'value' => 'NOTNULL',
//			),
//		)
//	);
//	return $field_filters;
//}, 10, 2 );

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.