Populate Dates as Choices

By default, this snippet will populate the next 14 “valid” dates as choices in any choice-based fields (e.g. Drop Down, Radio Buttons, Checkboxes, etc). Date validity is based on dates that are not specifically excluded or fall on days of the week that are excluded.

Code

Filename: gw-populate-dates-as-choices.php

<?php
/**
 * Gravity Wiz // Gravity Forms // Populate Dates as Choices
 * https://gravitywiz.com/
 *
 * By default, this snippet will populate the next 14 "valid" dates as choices in any choice-based fields (e.g. Drop Down,
 * Radio Buttons, Checkboxes, etc). Date validity is based on dates that are not specifically excluded or fall on days
 * of the week that are excluded.
 */
// Update "123" to your form ID.
add_filter( 'gform_pre_render_123', function( $form ) {

	// Update "4" to your choice field ID.
	$target_field_id = 4;

	// Update these dates to those you wish to specifically exclude (e.g. holidays).
	$excluded_dates = array(
		'2022-06-22',
		'2022-06-24',
		'2022-06-30',
		'2022-07-04',
	);

	// Update these to days of the week that should be excluded (typically weekend or week days).
	$excluded_days_of_week = array(
		'Saturday',
		'Sunday',
	);

	$prices = array( 2.33, 1.44, 0.89, 0.55, 0.34, 0.21, 0.13, 0.08, 0.05, 0.03, 0.02, 0.01 );

	foreach ( $form['fields'] as &$field ) {
		if ( $field->id != $target_field_id ) {
			continue;
		}
		$choices = array();
		$date    = new DateTime();
		$i       = 14;
		while ( $i > 0 ) {
			$date = $date->modify( '+1 day' );
			if ( in_array( $date->format( 'Y-m-d' ), $excluded_dates ) ) {
				continue;
			}
			if ( in_array( $date->format( 'l' ), $excluded_days_of_week ) ) {
				continue;
			}
			$price     = ! empty( $prices ) ? array_shift( $prices ) : 0;
			$choices[] = array(
				'text'  => $date->format( 'l, F jS' ),
				'value' => $date->format( 'Y-m-d' ),
				'price' => $price,
			);
			$i--;
		}
		$field->choices = $choices;
	}

	return $form;
} );

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.