对于预订网站,我已激活“时区”.应显示访客的当地时间.在预订表格中,一切正常.使用以下功能时,购物车数据($start_time,$end_time)显示不正确.
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>';
}
}
$start_time和$end_time的输出应显示在访问者的本地时间.我该如何实现?
解决方法:
要启用用户时区,必须使用jQuery和ajax.以下是以下步骤:
>尽早启用用户WooCommerce会话(WC Session),
>获取用户浏览器时区(用户计算机或设备的时区),
>通过Ajax将时区发送到php,
>在用户WC会话中获取该时区,
>然后在日期和时间的php函数代码中将其用作date(),date_i18n(),time()…
将用户时区保存在WC Session中后,所有相关代码将被自动禁用(不再需要).
编码:
// Early enable customer WC_Session (if not done)
add_action( 'init', 'wc_session_enabler' );
function wc_session_enabler() {
if ( ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
}
// Get the browser user timezone and send it to php (Ajax)
add_action( 'wp_head', 'user_timmezone_js' );
function user_timmezone_js() {
if( ! WC()->session->get('time-zone') ) :
?>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.6/jstz.min.js"></script>
<script type="text/javascript">
jQuery( function($){
if (typeof woocommerce_params === 'undefined')
return false;
var tz = jstz.determine();
$.ajax({
type: "POST",
url: woocommerce_params.ajax_url,
data: ({
'action': 'time_zone',
'timezone': tz.name()
}),
success: function(response) {
console.log('Success: '+response);
}
});
});
</script>
<?php
endif;
}
// function that gets the Ajax data and save it to user WC Session
add_action( 'wp_ajax_time_zone', 'set_session_timezone' );
add_action( 'wp_ajax_nopriv_time_zone', 'set_session_timezone' );
function set_session_timezone() {
if ( isset($_POST['timezone']) && ! empty($_POST['timezone']) ){
WC()->session->set('time-zone', esc_attr($_POST['timezone']) );
echo WC()->session->get('time-zone');
}
die();
}
代码进入您的活动子主题(或活动主题)的function.php文件中.
Then you will include the following to get and set the time zone:
06001
更新:
现在,使用woocommerce_get_item_data时,您的代码不正确,因为它是过滤器挂钩,需要返回过滤后的内容(而不是回显html)…
请尝试以下操作(未经测试):
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']) && ! is_admin() ) {
// Get and set the user time zone
if( $timezone = WC()->session->get('time-zone') ) {
date_default_timezone_set($timezone);
}
$booking_data = $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, $booking_data['_start_date'] ) );
$start_time = apply_filters( 'woocommerce_bookings_get_start_date_with_time', date_i18n( $time_format, $booking_data['_start_date'] ) );
$end_time = apply_filters( 'woocommerce_bookings_get_end_date_with_time', date_i18n( $time_format, $booking_data['_end_date'] ) );
$persons = $booking_data['_persons'];
$booking_id = $booking_data['_booking_id'];
$item_data[] = array(
'key' => __('Date', 'woocommerce'),
'value' => esc_html( $start_date ),
'display' => esc_html( $start_date ),
);
$item_data[] = array(
'key' => __('Time', 'woocommerce'),
'value' => esc_html( $start_time ),
'display' => esc_html( $start_time ),
);
$count = 1;
foreach($persons as $person){
$item_data[] = array(
'key' => __('Person', 'woocommerce') . $count,
'value' => esc_html( $person ),
'display' => esc_html( $person ),
);
$count++;
}
$item_data[] = array(
'key' => __('Booking #', 'woocommerce'),
'value' => esc_html( $booking_id ),
'display' => esc_html( $booking_id ),
);
}
return $item_data;
}
部分基于:How to detect user’s timezone?