Skip to content

Commit 084abdc

Browse files
authored
gpi-populate-days.php: Addes snippet to populate radio field with upcoming occurrences of a specific weekday, with configurable inventory limit and cutoff logic.
1 parent 91b6f40 commit 084abdc

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

gp-inventory/gpi-populate-days.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Inventory // Populate Days into Radio Field
4+
* https://gravitywiz.com/documentation/gravity-forms-inventory/
5+
*
6+
* Populate a Radio field with the next `n` days, and assign each choice to have `x` inventory.
7+
*
8+
* For example, you can populate a Radio field with the next 10 Thursdays, with each Thursday having an inventory of 25.
9+
* This is useful if you offer a service on a specific day only, and need users to select which date they want.
10+
*
11+
* Additionally, you can set a cutoff day and time for showing a day this week. For example, you can only show the current
12+
* week's Thursday if it is before 4pm on Tuesday. This is useful if you need to set a cut-off time for bookings this week.
13+
*
14+
* Instructions:
15+
*
16+
* 1. Install the snippet.
17+
* https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
18+
*
19+
* 2. Follow the inline instructions to configure the snippet for your form.
20+
*/
21+
// Change `123` to your form ID
22+
add_filter( 'gform_pre_render_123', 'gw_populate_days_into_radio', 5, 1 );
23+
add_filter( 'gform_pre_validation_123', 'gw_populate_days_into_radio', 5, 1 );
24+
function gw_populate_days_into_radio( $form ) {
25+
26+
// Update `1` with your Radio Button field ID
27+
$field_id = 1;
28+
29+
// Update `thursday` with the day you want to populate
30+
$day = 'thursday';
31+
32+
// Update `2` to your cut-off day for booking this week. Monday is 1, Tuesday is 2 etc.
33+
$cutoff_day = 2;
34+
35+
// Update `16` to your cut-off time for booking this week. The time is in 24 hour format, so 16 is 4pm.
36+
$cutoff_time = 16;
37+
38+
// Update `10` to the number of days to populate
39+
$number_of_days = 10;
40+
41+
// Update `25` to the inventory limit each day should have
42+
$inventory = 25;
43+
44+
// Update `l, F j, Y` to the PHP date format you want the populated days to be shown in.
45+
// More information about formats can be found here: https://www.php.net/manual/en/datetime.format.php
46+
$format = 'l, F j, Y';
47+
48+
// That's it, stop editing!
49+
50+
static $has_run = false;
51+
if ( $has_run ) {
52+
return $form;
53+
}
54+
$has_run = true;
55+
56+
foreach ( $form['fields'] as &$field ) {
57+
if ( $field->id == $field_id && $field->type == 'radio' ) {
58+
59+
$choices = array();
60+
$today = new DateTime();
61+
$start_day = new DateTime( 'this ' . $day );
62+
63+
// If it's past the cutoff, also skip this week's day
64+
if ( ( $today->format( 'N' ) == $cutoff_day && (int) $today->format( 'H' ) >= $cutoff_time ) || $today->format( 'N' ) > $cutoff_day && $today->format( 'N' ) <= $start_day->format( 'N' ) ) {
65+
$start_day->modify( '+1 week' );
66+
}
67+
68+
// Generate next n days
69+
for ( $i = 0; $i < $number_of_days; $i++ ) {
70+
$label = $start_day->format( $format );
71+
$choices[] = array(
72+
'text' => $label,
73+
'value' => $label,
74+
'inventory_limit' => $inventory,
75+
);
76+
$start_day->modify( '+1 week' );
77+
}
78+
79+
$field->choices = $choices;
80+
}
81+
}
82+
83+
return $form;
84+
}

0 commit comments

Comments
 (0)