-
Notifications
You must be signed in to change notification settings - Fork 92
gpi-populate-days.php: Added new snippet.
#1155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
107137b
2394f5c
1e78b4f
5a3b52a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,84 @@ | ||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Gravity Perks // Inventory // Populate Days into Radio Field | ||||||||||||||||||||||||
| * https://gravitywiz.com/documentation/gravity-forms-inventory/ | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * Populate a Radio field with the next `n` days, and assign each choice to have `x` inventory. | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * For example, you can populate a Radio field with the next 10 Thursdays, with each Thursday having an inventory of 25. | ||||||||||||||||||||||||
| * This is useful if you offer a service on a specific day only, and need users to select which date they want. | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * Additionally, you can set a cutoff day and time for showing a day this week. For example, you can only show the current | ||||||||||||||||||||||||
| * 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. | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * Instructions: | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * 1. Install the snippet. | ||||||||||||||||||||||||
| * https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * 2. Follow the inline instructions to configure the snippet for your form. | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| // Change `123` to your form ID | ||||||||||||||||||||||||
| add_filter( 'gform_pre_render_123', 'gw_populate_days_into_radio', 5, 1 ); | ||||||||||||||||||||||||
| add_filter( 'gform_pre_validation_123', 'gw_populate_days_into_radio', 5, 1 ); | ||||||||||||||||||||||||
| function gw_populate_days_into_radio( $form ) { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Update `1` with your Radio Button field ID | ||||||||||||||||||||||||
| $field_id = 1; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Update `thursday` with the day you want to populate | ||||||||||||||||||||||||
| $day = 'thursday'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Update `2` to your cut-off day for booking this week. Monday is 1, Tuesday is 2 etc. | ||||||||||||||||||||||||
| $cutoff_day = 2; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Update `16` to your cut-off time for booking this week. The time is in 24 hour format, so 16 is 4pm. | ||||||||||||||||||||||||
| $cutoff_time = 16; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Update `10` to the number of days to populate | ||||||||||||||||||||||||
| $number_of_days = 10; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Update `25` to the inventory limit each day should have | ||||||||||||||||||||||||
| $inventory = 25; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Update `l, F j, Y` to the PHP date format you want the populated days to be shown in. | ||||||||||||||||||||||||
| // More information about formats can be found here: https://www.php.net/manual/en/datetime.format.php | ||||||||||||||||||||||||
| $format = 'l, F j, Y'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // That's it, stop editing! | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| static $has_run = false; | ||||||||||||||||||||||||
| if ( $has_run ) { | ||||||||||||||||||||||||
| return $form; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| $has_run = true; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| foreach ( $form['fields'] as &$field ) { | ||||||||||||||||||||||||
| if ( $field->id == $field_id && $field->type == 'radio' ) { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| $choices = array(); | ||||||||||||||||||||||||
| $today = new DateTime(); | ||||||||||||||||||||||||
| $start_day = new DateTime( 'this ' . $day ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // If it's past the cutoff, also skip this week's day | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| $choices = array(); | |
| $today = new DateTime(); | |
| $start_day = new DateTime( 'this ' . $day ); | |
| // If it's past the cutoff, also skip this week's day | |
| $choices = array(); | |
| $tz = function_exists( 'wp_timezone' ) ? wp_timezone() : null; | |
| $today = new DateTime( 'now', $tz ); | |
| $start_day = new DateTime( 'this ' . $day, $tz ); | |
| // If it's past the cutoff, also skip this week's day |
🧰 Tools
🪛 GitHub Check: PHPCS (Files Changed)
[warning] 60-60:
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 3 spaces
[warning] 59-59:
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
🤖 Prompt for AI Agents
In gp-inventory/gpi-populate-days.php around lines 59 to 63, the DateTime
instances are created without specifying the WordPress timezone causing
potential server vs WP timezone drift; change the DateTime creation to use
wp_timezone() so both $today and $start_day are initialized with the site
timezone (i.e., construct them using the WordPress timezone object returned by
wp_timezone()) to ensure consistent date calculations.
Check failure on line 64 in gp-inventory/gpi-populate-days.php
GitHub Actions / PHPCS (Files Changed)
Expected 1 spaces after opening parenthesis; 0 found
Check failure on line 64 in gp-inventory/gpi-populate-days.php
GitHub Actions / PHPCS (Files Changed)
Expected 1 spaces before closing parenthesis; 0 found
Check failure on line 64 in gp-inventory/gpi-populate-days.php
GitHub Actions / PHPCS (Files Changed)
Expected 1 spaces after opening parenthesis; 0 found
Check failure on line 64 in gp-inventory/gpi-populate-days.php
GitHub Actions / PHPCS (Files Changed)
Expected 1 space(s) after cast statement; 0 found
Check failure on line 64 in gp-inventory/gpi-populate-days.php
GitHub Actions / PHPCS (Files Changed)
Expected 1 spaces before closing parenthesis; 0 found
Uh oh!
There was an error while loading. Please reload this page.