Skip to content

Instantly share code, notes, and snippets.

@LiamBailey
Created March 10, 2016 18:25
Show Gist options
  • Save LiamBailey/417e8bf233c7cd7c0fb8 to your computer and use it in GitHub Desktop.
Save LiamBailey/417e8bf233c7cd7c0fb8 to your computer and use it in GitHub Desktop.
Changes out the Woocommerce Bookings date picker fields for a nice dropdown showing only available dates
<?php
/*
Plugin Name: Woocommerce Bookings Dropdown
Description: Swaps the date picker for a dropdown of dates
Version: 1.0.0
Author: Webby Scots
Author URI: http://webbyscots.com/
*/
$wswp_dates_built = false;
add_filter('booking_form_fields','wswp_booking_form_fields');
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');
$i++;
$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');
}
$i++;
}
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);
}
}
ksort($dates);
foreach($dates as $key => $date) {
$dates[date("Y-m-d",$key)] = $date;
unset($dates[$key]);
}
$wswp_dates_built = true;
return $dates;
}
add_action('wp_footer','wswp_css_js');
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 {
display:none;
}
</style>
<script type='text/javascript'>
jQuery(function($) {
$(".picker-chooser").insertBefore('.wc-bookings-date-picker-date-fields');
$("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] );
});
});
</script>
<?php
}
@pixeltherapy
Copy link

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