-
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
107137b
`gpi-populate-days.php`: Added new snippet.
matty0501 2394f5c
`gpi-populate-days.php`: Added new snippet.
saifsultanc 1e78b4f
`gpi-populate-days.php`: Added new snippet.
saifsultanc 5a3b52a
`gpi-populate-days.php`: Added new snippet.
saifsultanc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| if ( ( $today->format( 'N' ) == $cutoff_day && ( int )$today->format( 'H' ) >= $cutoff_time ) || $today->format( 'N' ) > $cutoff_day && $today->format( 'N' ) <= $start_day->format( 'N' ) ) { | ||
|
Check failure on line 64 in gp-inventory/gpi-populate-days.php
|
||
| $start_day->modify( '+1 week' ); | ||
| } | ||
|
|
||
| // Generate next n days | ||
| for ( $i = 0; $i < $number_of_days; $i++ ) { | ||
| $label = $start_day->format( $format ); | ||
| $choices[] = array( | ||
| 'text' => $label, | ||
| 'value' => $label, | ||
| 'inventory_limit' => $inventory, | ||
| ); | ||
| $start_day->modify('+1 week'); | ||
|
Check failure on line 76 in gp-inventory/gpi-populate-days.php
|
||
| } | ||
|
|
||
| $field->choices = $choices; | ||
| } | ||
| } | ||
|
|
||
| return $form; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.