WooCommerce 预订:在购物车和结账时添加和重新排序项目数据
WooCommerce Bookings: adding and reordering item data in cart and checkout
我想显示预订的结束日期和开始日期。到目前为止,我有以下内容:
add_filter( 'woocommerce_get_item_data', 'display_booking_date', 10, 2 );
function display_booking_date( $item_data, $cart_item ){
if ( ! empty( $cart_item['booking'] ) ) {
$date_format = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() );
$end_date = apply_filters( 'woocommerce_bookings_get_end_date_with_time', date_i18n( $date_format, $cart_item['booking']['_end_date'] ) );
$item_data[] = array(
'key' => __( 'End Date', 'your-textdomain' ),
'value' => $cart_item['booking']['_end_date'],
'display' => $end_date,
);
}
return $item_data;
}
这允许我显示预订的结束日期。但是,这是在我不想要的任何其他元数据之后。有什么办法可以:
1) 更改结束日期的显示位置,使其出现在开始日期之后,而不是在所有其他元数据之后
2) 更好的做法是将开始日期和结束日期放在一起作为元数据的同一位。
谢谢!
Code lightly updated
是的,这是可能的,但您必须知道 key
的 'Start Date' 的项目数据……在这里,我重新排序该项目数据。
But is not possible to have the start and end date together (because you can only have in the array 1 key, 1 value and 1 display). What is may be possible is to merge the both values and the both displays together.
如果需要,要显示 $item_data
参数的原始数据,您可以在您的内部使用 print_r($item_data);
在最后的 return
之前的函数。
代码如下:
add_filter( 'woocommerce_get_item_data', 'display_booking_date', 10, 2 );
function display_booking_date( $item_data, $cart_item ){
if ( ! empty( $cart_item['booking'] ) ) {
// ==> HERE define your real START DATE 'name'
$item_data_start_date_name = __('Start Date', 'your-textdomain' ); // or just 'Start Date' …
$date_format = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() );
$end_date = apply_filters( 'woocommerce_bookings_get_end_date_with_time', date_i18n( $date_format, $cart_item['booking']['_end_date'] ) );
$item_data_end_date_array = array(
'key' => __( 'End Date', 'your-textdomain' ),
'value' => $cart_item['booking']['_end_date'],
'display' => $end_date,
);
$new_item_data = array();
$count = 0;
//iteratting throug each item_data values
foreach($item_data as $key => $value){
// If we find the "start date" we set just afer the "end date"
if( $value['name'] == $item_data_start_date_name ){
$new_item_data[$count] = $value;
$count++;
$new_item_data[$count] = $item_data_end_date_array;
$count++;
} else {
$new_item_data[$count] = $value;
$count++;
}
}
return $new_item_data;
} else {
return $item_data;
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
该代码已经过其他设置测试,应该适合您。
我想显示预订的结束日期和开始日期。到目前为止,我有以下内容:
add_filter( 'woocommerce_get_item_data', 'display_booking_date', 10, 2 );
function display_booking_date( $item_data, $cart_item ){
if ( ! empty( $cart_item['booking'] ) ) {
$date_format = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() );
$end_date = apply_filters( 'woocommerce_bookings_get_end_date_with_time', date_i18n( $date_format, $cart_item['booking']['_end_date'] ) );
$item_data[] = array(
'key' => __( 'End Date', 'your-textdomain' ),
'value' => $cart_item['booking']['_end_date'],
'display' => $end_date,
);
}
return $item_data;
}
这允许我显示预订的结束日期。但是,这是在我不想要的任何其他元数据之后。有什么办法可以:
1) 更改结束日期的显示位置,使其出现在开始日期之后,而不是在所有其他元数据之后
2) 更好的做法是将开始日期和结束日期放在一起作为元数据的同一位。
谢谢!
Code lightly updated
是的,这是可能的,但您必须知道 key
的 'Start Date' 的项目数据……在这里,我重新排序该项目数据。
But is not possible to have the start and end date together (because you can only have in the array 1 key, 1 value and 1 display). What is may be possible is to merge the both values and the both displays together.
如果需要,要显示 $item_data
参数的原始数据,您可以在您的内部使用 print_r($item_data);
在最后的 return
之前的函数。
代码如下:
add_filter( 'woocommerce_get_item_data', 'display_booking_date', 10, 2 );
function display_booking_date( $item_data, $cart_item ){
if ( ! empty( $cart_item['booking'] ) ) {
// ==> HERE define your real START DATE 'name'
$item_data_start_date_name = __('Start Date', 'your-textdomain' ); // or just 'Start Date' …
$date_format = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() );
$end_date = apply_filters( 'woocommerce_bookings_get_end_date_with_time', date_i18n( $date_format, $cart_item['booking']['_end_date'] ) );
$item_data_end_date_array = array(
'key' => __( 'End Date', 'your-textdomain' ),
'value' => $cart_item['booking']['_end_date'],
'display' => $end_date,
);
$new_item_data = array();
$count = 0;
//iteratting throug each item_data values
foreach($item_data as $key => $value){
// If we find the "start date" we set just afer the "end date"
if( $value['name'] == $item_data_start_date_name ){
$new_item_data[$count] = $value;
$count++;
$new_item_data[$count] = $item_data_end_date_array;
$count++;
} else {
$new_item_data[$count] = $value;
$count++;
}
}
return $new_item_data;
} else {
return $item_data;
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
该代码已经过其他设置测试,应该适合您。