Hey guys,
Ok so i am trying to change my date inputs in Toolset form to allow date selections only on dates that are available, There is a hook that generates a list of unavailable dates for me...
It looks like the date generation is working correctly, butwhen I submit, it keeps telling me that the 2 date fields are empty even though dates are selected...
I would love some assistance, The hook code is as follows 🙂
<?php
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
add_shortcode( 'item_available_from', function( $atts ){
global $post;
global $wpdb;
$postid = (isset($atts['equipment_id']) ? $atts['equipment_id'] : $post->ID);
$date_ranges = [];
$child_posts = toolset_get_related_posts($postid, 'equipment-booking-request', array( 'query_by_role' => 'parent', 'return' => 'post_object' ) );
foreach($child_posts as $post){
$post_data = (array) get_post_meta( $post->ID);
if($post_data['wpcf-booking-status'][0]=="Approved" || $post_data['wpcf-booking-status'][0]=="Pending"){
$date_ranges[] = [
'start_date' => $post_data['wpcf-starting-date'][0],
'end_date' => $post_data['wpcf-end-date'][0],
];
}
}
$date = '';
$date_plus = 0;
while($date==''){
$date_plus++;
$found = false;
foreach($date_ranges as $date_range){
if((time() + (86400*$date_plus)>=$date_range['start_date']) && (time() + (86400*$date_plus)<=$date_range['end_date'])){
$found = true;
break;
}
}
if($found == false) $date = date("Y-m-d", time() + (86400*$date_plus));
}
$disabled_dates = [];
$disabled_dates_string = [];
foreach($date_ranges as $date_range){
$current = $date_range['start_date'];
while($current<$date_range['end_date']){
$cur_date = date('j-n-Y', $current);
if(!in_array($cur_date, $disabled_dates)) $disabled_dates[] = $cur_date;
$current+=86400;
}
}
$disabled_dates_string = '[';
foreach($disabled_dates as $key => $date_item){
$disabled_dates_string .= ($key>0 ? ',"'.$date_item.'"' : '"'.$date_item.'"');
}
$disabled_dates_string.= ']';
echo "<script>
function get_available_dates(date) {
var unavailableDates = ".json_encode($disabled_dates).";
dmy = date.getDate() + \"-\" + (date.getMonth()+1) + \"-\" + date.getFullYear();
if (jQuery.inArray(dmy, unavailableDates) < 0) {
return [true, 'available'];
} else {
return [false, 'unavailable'];
}
}
jQuery(document).on('cred_form_ready', function() {
jQuery('.js-wpt-date[name^=\"wpcf-end-date\"]').datepicker('option', 'minDate', 0);
jQuery('.js-wpt-date[name^=\"wpcf-starting-date\"]').datepicker('option', 'minDate', 0);
jQuery('.js-wpt-date[name^=\"wpcf-end-date\"]').datepicker( 'option', 'beforeShowDay', get_available_dates);
jQuery('.js-wpt-date[name^=\"wpcf-starting-date\"]').datepicker( 'option', 'beforeShowDay', get_available_dates);
});
</script>";
echo "<script>
jQuery(document).on('cred_form_ready', function() {
var unavailableDates = ".json_encode($disabled_dates).";
jQuery('.js-wpt-date[name^=\"wpcf-end-date\"]').datepicker({
minDate: 0,
beforeShowDay: function(date) {
dmy = date.getDate() + \"-\" + (date.getMonth()+1) + \"-\" + date.getFullYear();
if (jQuery.inArray(dmy, unavailableDates) < 0) {
return [true, 'available'];
} else {
return [false, 'unavailable'];
}
}
});
jQuery('.js-wpt-date[name^=\"wpcf-starting-date\"]').datepicker({
minDate: 0,
beforeShowDay: function(date) {
dmy = date.getDate() + \"-\" + (date.getMonth()+1) + \"-\" + date.getFullYear();
if (jQuery.inArray(dmy, unavailableDates) < 0) {
return [true, 'available'];
} else {
return [false, 'unavailable'];
}
}
});
});</script>";
return $date;
});