Hi all, I’m not a programmer and I want to check if this is achievable by Gravity form.
I want to use Gravity form to build a registration form for physical first aid training. As courses can span one to four days. If I want to create a 3 days event, and user will need to choose the start date and fill in their particular. Upon submitting the registration form, 2nd and 3rd date will auto-populate based on chosen start date. Is this possible?
Next we need a start date ( date field ) and then add day 2, day 3 and day 4 date field and make them hidden ( check the screenshot above ).
Then add the following code in your functions.php file and make sure you replace the field ids. You only need to change the values of field IDs present at the beginning of the code and the rest of the code will work automatically.
add_action( 'gform_after_submission', 'wpmonks_update_event_dates', 10, 2 );
function wpmonks_update_event_dates( $entry, $form ) {
// Declare variables for field IDs
$number_of_days_field_id = 1; // Field ID for number of days
$start_date_field_id = 3; // Field ID for start date
$second_day_field_id = 4; // Field ID for the 2nd day
$third_day_field_id = 5; // Field ID for the 3rd day
$fourth_day_field_id = 6; // Field ID for the 4th day
// Getting the start date from the entry
$start_date = rgar( $entry, $start_date_field_id );
// Number of days for the event
$number_of_days = (int) rgar( $entry, $number_of_days_field_id );
// Convert the start date to a DateTime object
$start_date_obj = DateTime::createFromFormat('Y-m-d', $start_date);
// Check if the date conversion was successful
if ( $start_date_obj !== false ) {
// Loop through the number of days to set subsequent dates
for ( $i = 2; $i <= $number_of_days; $i++ ) {
// Add days to the start date
$new_date = clone $start_date_obj;
$new_date->modify('+' . ($i - 1) . ' day');
// Update entry with new dates
switch( $i ) {
case 2:
// Update the entry for the 2nd day
GFAPI::update_entry_field( $entry['id'], $second_day_field_id, $new_date->format('Y-m-d') );
break;
case 3:
// Update the entry for the 3rd day
GFAPI::update_entry_field( $entry['id'], $third_day_field_id, $new_date->format('Y-m-d') );
break;
case 4:
// Update the entry for the 4th day
GFAPI::update_entry_field( $entry['id'], $fourth_day_field_id, $new_date->format('Y-m-d') );
break;
}
}
}
}