Skip to content

Instantly share code, notes, and snippets.

@lmeyer
Created January 28, 2017 12:07
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 lmeyer/a1bde79920239c21954e9061bb4e050a to your computer and use it in GitHub Desktop.
Save lmeyer/a1bde79920239c21954e9061bb4e050a to your computer and use it in GitHub Desktop.
Usage of woocommerce_appointments_time_slots_html filter (display left spaces when no booking)
<?php
add_filter('woocommerce_appointments_time_slots_html', 'idl_woocommerce_appointments_time_slots_html', 10, 8);
function idl_woocommerce_appointments_time_slots_html($slot_html, $slots, $intervals, $time_to_check, $staff_id, $from, $timezone, $appointment) {
if ( empty( $intervals ) ) {
$default_interval = 'hour' === $appointment->get_duration_unit() ? $appointment->get_duration() * 60 : $appointment->get_duration();
$custom_interval = 'hour' === $appointment->get_duration_unit() ? $appointment->get_duration() * 60 : $appointment->get_duration();
if ( $appointment->get_interval_unit() && $appointment->get_interval() ) {
$custom_interval = 'hour' === $appointment->get_interval_unit() ? $appointment->get_interval() * 60 : $appointment->get_interval();
}
$intervals = array( $default_interval, $custom_interval );
}
list( $interval, $base_interval ) = $intervals;
$start_date = current( $slots );
$end_date = end( $slots );
$slots = $appointment->get_available_slots( $slots, $intervals, $staff_id, $from );
$slot_html = '';
if ( $slots ) {
// Timezones
$timezone_datetime = new DateTime();
$local_time = wc_appointment_timezone_locale( 'site', 'user', $timezone_datetime->getTimestamp(), wc_time_format(), $timezone );
$site_time = wc_appointment_timezone_locale( 'site', 'user', $timezone_datetime->getTimestamp(), wc_time_format(), wc_timezone_string() );
// Split day into three parts
$times = apply_filters( 'woocommerce_appointments_times_split', array(
"morning" => array(
"name" => __( 'Morning', 'woocommerce-appointments' ),
"from" => strtotime("00:00"),
"to" => strtotime("12:00"),
),
"afternoon" => array(
"name" => __( 'Afternoon', 'woocommerce-appointments' ),
"from" => strtotime("12:00"),
"to" => strtotime("17:00"),
),
"evening" => array(
"name" => __( 'Evening', 'woocommerce-appointments' ),
"from" => strtotime("17:00"),
"to" => strtotime("24:00"),
),
));
$slot_html .= "<div class=\"slot_row\">";
foreach( $times as $k => $v ) {
$slot_html .= "<ul class=\"slot_column $k\">";
$slot_html .= '<li class="slot_heading">' . $v['name'] . '</li>';
$count = 0;
foreach ( $slots as $slot ) {
if ( $v['from'] <= strtotime( date( 'G:i', $slot ) ) && $v['to'] > strtotime( date( 'G:i', $slot ) ) ) {
$selected = date( 'G:i', $slot ) == date( 'G:i', $time_to_check ) ? ' selected' : '';
// Test availability for each slot.
$test_availability = $appointment->get_available_appointments( $slot, strtotime( "+{$interval} minutes", $slot ), $staff_id, 1 );
//$test_availability = 1;
//var_dump( date( 'G:i', $slot ) );
// Return available qty for each slot.
if ( ! is_wp_error( $test_availability ) ) {
if ( is_array( $test_availability ) ) {
$available_qty = max( $test_availability );
} else {
$available_qty = $test_availability;
}
} else {
$available_qty = 0;
}
// Disply each slot HTML.
if ( $available_qty > 0 ) {
$slot_left = " <small class=\"spaces-left\">(" . sprintf( _n( '%d left', '%d left', $available_qty, 'woocommerce-appointments' ), absint( $available_qty ) ) . ")</small>";
$slot_locale = ( $local_time !== $site_time ) ? sprintf( __( ' data-locale="Your local time: %s"', 'woocommerce-appointments' ), wc_appointment_timezone_locale( 'site', 'user', $slot, wc_date_format() . ', ' . wc_time_format(), $timezone ) ) : '';
$slot_html .= "<li class=\"slot$selected\"$slot_locale><a href=\"#\" data-value=\"" . date_i18n( 'G:i', $slot ) . "\">" . date_i18n( wc_time_format(), $slot ) . "$slot_left</a></li>";
} else {
continue;
}
} else {
continue;
}
$count++;
}
if ( ! $count ) {
$slot_html .= '<li class="slot slot_empty">' . __( '&#45;', 'woocommerce-appointments' ) . '</li>';
}
$slot_html .= "</ul>";
}
$slot_html .= "</div>";
}
return $slot_html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment