Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Changes out the Woocommerce Bookings date picker fields for a nice dropdown showing only available dates
Plugin Name: Woocommerce Bookings Dropdown
Description: Swaps the date picker for a dropdown of dates
Version: 1.0.0
Author: Webby Scots
Author URI:
$wswp_dates_built = false;
function wswp_booking_form_fields($fields) {
global $wswp_dates_built;
$i = 0;
foreach($fields as $field) {
$new_fields[$i] = $field;
if ($field['type'] == "date-picker" && $wswp_dates_built === false)
$new_fields[$i]['class'] = array('picker-hidden');
$new_fields[$i] = $field;
$new_fields[$i]['type'] = "select";
$new_fields[$i]['options'] = wswp_build_options($field['availability_rules'][0]);
$new_fields[$i]['class'] = array('picker-chooser');
return $new_fields;
function wswp_build_options($rules,$building = false) {
global $wswp_dates_built;
$dates = array();
foreach($rules as $dateset) {
if ($dateset[0] == "custom") {
$year = reset(array_keys($dateset[1]));
$month = reset(array_keys($dateset[1][$year]));
$day = reset(array_keys($dateset[1][$year][$month]));
$dtime = strtotime($year."-".$month."-".$day);
$dates[$dtime] = date("d/m/Y",$dtime);
foreach($dates as $key => $date) {
$dates[date("Y-m-d",$key)] = $date;
$wswp_dates_built = true;
return $dates;
function wswp_css_js() {
//adding in footer as not enough to justify new stylesheet and js file
?><style type="text/css">
.picker-hidden .picker,.picker-hidden legend {
<script type='text/javascript'>
jQuery(function($) {
$("select#wc_bookings_field_start_date").on('change', function() {
var selectedDate = $(this).val()
var selectedDateBreakdown = selectedDate.split("-");
$( "input[name*='wc_bookings_field_start_date_year']" ).val( selectedDateBreakdown[0] );
$( "input[name*='wc_bookings_field_start_date_month']" ).val( selectedDateBreakdown[1] );
$( "input[name*='wc_bookings_field_start_date_day']" ).val( selectedDateBreakdown[2] );
Copy link

pixeltherapy commented Apr 5, 2016

Thanks for the gist!

Note that this only populates the select box with Range Type: Date Range rules rather than Range Type: Range of Days/Weeks/Months rules. This doesn't throw errors but simply returns a blank array. Nevertheless, this is a great boost on how to extend WooCommerce Bookings when there's precious little information elsewhere.

doffs cap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment