Skip to content
Advertisement

Enable user local time in Woocommerce Cart and Checkout date time display

For a bookings website I have activated ‘Timezones’. The visitor’s local time should be displayed. In the booking form everything is working fine. The cart data ($start_time, $end_time) is not being displayed correctly when using the function below.

add_filter( 'woocommerce_get_item_data', 'display_cart_data_wc_bookings', 10, 2 );
function display_cart_data_wc_bookings( $item_data, $cart_item ){

    if ( ! empty( $cart_item['booking'] ) ) {

        $date_format = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() );
        $time_format = apply_filters( 'woocommerce_bookings_time_format', wc_time_format() );
        $start_date = apply_filters( 'woocommerce_bookings_get_start_date_with_time', date_i18n(  $date_format, $cart_item['booking']['_start_date'] ) );
        $start_time = apply_filters( 'woocommerce_bookings_get_start_date_with_time', date_i18n(  $time_format, $cart_item['booking']['_start_date'] ) );
        $end_time = apply_filters( 'woocommerce_bookings_get_end_date_with_time', date_i18n(  $time_format, $cart_item['booking']['_end_date'] ) );
        $persons = $cart_item['booking']['_persons'];
        $ID = $cart_item['booking']['_booking_id'];

        echo '<dt class="titel datum">' . __('Date', 'woocommerce') . '</dt><dd class="data datum">' . esc_html( $start_date ) . '</dd>';
        echo '<dt class="titel tijdvak">' . __('Time', 'woocommerce') . '</dt><dd class="data tijdvak">' . esc_html( $start_time ) . ' - ' . esc_html( $end_time ) . '</dd>';
        foreach($persons as $person){
            echo '<dt class="titel personen">' . __('Persons', 'woocommerce') . '</dt><dd class="data personen">' . esc_html( $person) . '</dd>';
        }
        echo '<dt class="titel booking_id">' . __('Booking #', 'woocommerce') . '</dt><dd class="data booking_id">' . esc_html( $ID) . '</dd>';

    }
}

The output of $start_time and $end_time should be displayed in the visitor’s local time. How can I achieve this?

Advertisement

Answer

@LoicTheAztec: Thanks for the update. Still not working. I’ve updated the code based on file ‘class-wc-booking-cart-manager.php’:

add_filter( 'woocommerce_get_item_data', 'display_cart_data_wc_bookings', 10, 2 );
function display_cart_data_wc_bookings( $item_data, $cart_item ){
    if ( isset($cart_item['booking']) && ! empty($cart_item['booking']) ) {

        // Get and set the user time zone
        $booking_data = $cart_item['booking'];
        if ( ! empty( $booking_data['_booking_id'] ) ) {
            $booking = get_wc_booking( $booking_data['_booking_id'] );
            if ( wc_should_convert_timezone( $booking ) ) {
                $timezone_data = array(
                    'name'    => get_wc_booking_data_label( 'timezone', $cart_item['data'] ),
                    'value'   => str_replace( '_', ' ', $booking->get_local_timezone() ),
                    'display' => '',
                );
            }
        }

        $date_format    = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() );
        $time_format    = apply_filters( 'woocommerce_bookings_time_format', wc_time_format() );
        $start_date     = date_i18n( get_option( 'date_format' ), $booking->get_start( 'view', true ) );
        $start_time     = date_i18n( get_option( 'time_format' ), $booking->get_start( 'view', true ) );
        $end_time       = date_i18n( get_option( 'time_format' ), $booking->get_end( 'view', true ) );
        $persons        = $booking_data['_persons'];
        $booking_id     = $booking_data['_booking_id']; 

        $item_data[] = array(
            'key'       => __('Booking #', 'woocommerce'),
            'value'     => esc_html( $booking_id ),
            'display'   => esc_html( $booking_id ),
        );

        $item_data[] = array(
            'key'       => __('Date', 'woocommerce'),
            'value'     => esc_html( $start_date ),
            'display'   => esc_html( $start_date ),
        );

        $item_data[] = array(
            'key'       => __('Start time', 'woocommerce'),
            'value'     => esc_html( $start_time ),
            'display'   => esc_html( $start_time ),
        );

        $item_data[] = array(
            'key'       => __('End time', 'woocommerce'),
            'value'     => esc_html( $end_time ),
            'display'   => esc_html( $end_time ),
        );

        $count = 1;

        foreach($persons as $person){
            $item_data[] = array(
                'key'       => __('Person(s)', 'woocommerce') . $count,
                'value'     => esc_html( $person ),
                'display'   => esc_html( $person ),
            );
            $count++;
        }

        if ( ! empty( $timezone_data ) ) {
            // Add timezone to the end.
            $item_data[] = $timezone_data;
        }
    }

    return $item_data;

With this code step 1 to step 4 in your first comment are not necessary. Tested and it works!

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement