Code Snippet for switching User and Store time on frontend not working

Issue #353 resolved
Valentina R created an issue

This snippet does not work anymore: https://bizzthemes.com/forums/topic/what-is-the-logic-behind-showing-2-timezones-to-the-user/#post-148338

It displays the slots in the customer timezone as expected, but they are all unclickable.

You can test it here. But first set the timezone of WP to Adelaide. Try the 07.05 - any hour: http://fable.valentina-rakar.com/product/test-staff-gcal/

Comments (4)

  1. Valentina R reporter

    You don't even need to switch the timezone. It can be the same. Just actiavte this code snippet: Swicth user and store timezone display in Calendar I checked and the HTML generated is the same as without using this snippet.

  2. Željan Topić

    Try this snippet:

    add_filter( 'woocommerce_appointments_time_slot_html', 'custom_time_slot_html', 10, 8 );
    function custom_time_slot_html( $slot_html, $slot, $quantity, $time_to_check, $staff_id, $timezone, $appointable_product, $spaces_left ) {
        // 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() );
    
        // Variables.   
        $selected       = $time_to_check && date( 'G:i', $slot ) === date( 'G:i', $time_to_check ) ? ' selected' : '';
        $slot_locale    = ( $local_time !== $site_time ) ? sprintf( __( ' data-locale="Store local time: %s"', 'woocommerce-appointments' ), date_i18n( wc_time_format(), $slot ) ) : '';
        $slot_user_time = ( $local_time !== $site_time ) ? wc_appointment_timezone_locale( 'site', 'user', $slot, wc_date_format() . ', ' . wc_time_format(), $timezone ) : date_i18n( wc_time_format(), $slot );
    
        if ( $quantity['scheduled'] ) {
            /* translators: 1: quantity available */
            $slot_html = "<li class=\"slot$selected\"$slot_locale data-slot=\"" . esc_attr( date( 'Hi', $slot ) ) . "\"><a href=\"#\" data-value=\"" . date_i18n( 'G:i', $slot ) . "\">" . $slot_user_time . " <small class=\"spaces-left\">" . $spaces_left . "</small></a></li>";
        } else {
            $slot_html = "<li class=\"slot$selected\"$slot_locale data-slot=\"" . esc_attr( date( 'Hi', $slot ) ) . "\"><a href=\"#\" data-value=\"" . date_i18n( 'G:i', $slot ) . "\">" . $slot_user_time . "</a></li>";
        }
    
        return $slot_html;
    }
    
  3. Log in to comment