Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zarankumar/44f693498adc76f763aa2fff32855a06 to your computer and use it in GitHub Desktop.
Save zarankumar/44f693498adc76f763aa2fff32855a06 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment