Skip to content

Instantly share code, notes, and snippets.

@PluginHive
Created June 27, 2019 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PluginHive/946e67bab6f1dda9f49627548ff39f12 to your computer and use it in GitHub Desktop.
Save PluginHive/946e67bab6f1dda9f49627548ff39f12 to your computer and use it in GitHub Desktop.
Change Your calendar starting date based on your availability rule.
/**
* Change Your calendar starting date based on your availability rule.
* Created at : 27th June 2019
*/
add_filter( 'ph_booking_calendar_start_date', 'ph_booking_calendar_start_date',10, 2 );
function ph_booking_calendar_start_date($start_date,$product_id)
{
// return $message;
$fixed_availability_from = get_post_meta( $product_id, "_phive_fixed_availability_from", 1 );
$fixed_availability_to = get_post_meta( $product_id, "_phive_fixed_availability_to", 1 );
$availabiliy_rules = get_post_meta( $product_id, '_phive_booking_availability_rules', 1 );
$un_availabiliy = get_post_meta( $product_id, '_phive_un_availability', 1 );
$availabiliy_rules = !empty($availabiliy_rules) ? $availabiliy_rules : array();
$availability_dates=array();
$calendar_for='date-picker';
$min_avail_date=9999999999;
foreach ($availabiliy_rules as $key => $rule) {
if( $rule['availability_type']=='custom' ){
if($calendar_for == 'time-picker' || $calendar_for == 'date-picker'){
$date_from = explode(" ",$rule['from_date']);
$date_from = $date_from[0];
$date_to = explode(" ",$rule['to_date']);
$date_to = $date_to[0];
}
else{
$date_from = $rule['from_date'];
$date_to = $rule['to_date'];
}
$date_from_str=strtotime( $date_from );
$availability_dates[$date_from] = isset($availability_dates[$date_from])?$availability_dates[$date_from]:$rule['is_bokable'];
$min_avail_date=($min_avail_date>$date_from_str && $rule['is_bokable']=='yes')?$date_from_str:$min_avail_date;
while( strtotime( $date_from ) <= strtotime( $date_to ) )
{
$date_from = date( 'Y-m-d',strtotime( "+1 day", strtotime($date_from) ) );
$date_from_str=strtotime($date_from);
$min_avail_date=($min_avail_date>$date_from_str && $rule['is_bokable']=='yes')?$date_from_str:$min_avail_date;
$availability_dates[$date_from] = isset($availability_dates[$date_from])?$availability_dates[$date_from]:$rule['is_bokable'];
}
}
}
ksort($availability_dates);
$timezone = get_option('timezone_string');
if( empty($timezone) ){
$time_offset = get_option('gmt_offset');
$timezone= timezone_name_from_abbr( "", $time_offset*60*60, 0 );
}
date_default_timezone_set($timezone);
$current_date=strtotime(date('Y-m-1'));
// Start date
if( !empty($fixed_availability_from) && strtotime($fixed_availability_from) >= $current_date ){
$start_date = date( 'Y', strtotime($fixed_availability_from) ).'-'.date( 'm', strtotime($fixed_availability_from) ).'-01';
}else{
if($un_availabiliy=='no')
{
$today=date('Y-m-d');
if(isset($availability_dates[$today]) && $availability_dates[$today]!='yes')
{
foreach ($availability_dates as $date => $status) {
if(strtotime($today) >= strtotime($date) )
{
continue;
}
else
{
$next_day=date('Y-m-d', strtotime('+1 day', strtotime($today)));
if(strtotime($next_day)!=strtotime($date))
{
$start_date=date('Y-m-d',strtotime($next_day));
$start_date = date( 'Y', strtotime($start_date) ).'-'.date( 'm', strtotime($start_date) ).'-01';
}
else
{
$today=$next_day;
}
}
}
}
else
{
$start_date =date('Y').'-'.date('m').'-01';
}
}
else if($min_avail_date!=9999999999)
{
$start_date=date('Y-m-d',$min_avail_date);
$start_date = date( 'Y', strtotime($start_date) ).'-'.date( 'm', strtotime($start_date) ).'-01';
// $start_date = date('Y').'-'.date('m').'-01';
}
else
{
$start_date = date('Y').'-'.date('m').'-01';
}
}
return $start_date;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment