在 Woocommerce 预订中的购物车项目名称下方显示特定预订数据
Display specific booking data below cart item name in Woocommerce Bookings
我在别墅预订网站上使用 Woocommerce 和 Woocommerce Bookings,但我有一个小问题无法解决。
我想将字段 "Details" 添加到我的购物车 (Cart Details)。此字段将显示预订的持续时间以及每个别墅的价格/晚。
我的别墅有一些具有特定区块成本的资源。
关于持续时间值,我可以使用以下代码显示它:
<?php
/*display_card_data();*/
$items = WC()->cart->get_cart();
foreach($items as $item) {
$duration = $item['booking']['duration'];
}
// displaying values for test
echo $duration. ' x Night Price' .$price ;
?>
我想知道如何在此字段中显示块成本。
请提供任何帮助。
这可以通过以下代码完成(但在 WC Bookings 中它们可以是许多单价):
add_filter( 'woocommerce_cart_item_name', 'booking_details_after_name', 30, 3 );
function booking_details_after_name( $product_name, $cart_item, $cart_item_key ) {
if ( isset( $cart_item['booking']['duration'] ) ){
// Duration
$duration = $cart_item['booking']['duration'];
// Price cost ( they can be many different )
$base_cost = get_post_meta( $cart_item['product_id'], '_wc_booking_cost', true );
$block_cost = get_post_meta( $cart_item['product_id'], '_wc_booking_block_cost', true );
// Output
$product_name .= '<br><span class="booking-details">';
$product_name .= $duration . __(" x Night Price ", "woocommerce") . wc_price($base_cost);
$product_name .= '</span>';
}
return $product_name;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。
具体更新:
add_filter( 'woocommerce_cart_item_name', 'booking_details_after_name', 30, 3 );
function booking_details_after_name( $product_name, $cart_item, $cart_item_key ) {
if ( isset( $cart_item['booking']['duration'] ) ){
// Duration
$duration = (int) $cart_item['booking']['_duration'];
$resource_id = $cart_item['booking']['_resource_id'];
$start_time = $cart_item['booking']['_start_date'];
$end_time = $cart_item['booking']['_end_date'];
$loop_time = (int) $start_time;
$day = 86400; // In seconds
// Price cost ( they can be many different )
$res_block_cost = get_post_meta( $cart_item['product_id'], '_resource_block_costs', true );
$booking_pricing = get_post_meta( $cart_item['product_id'], '_wc_booking_pricing', true );
foreach ( $res_block_cost as $key => $value ){
if( $key == $resource_id ){
$bloc_cost = $value;
break;
}
}
$cost = array();
foreach ( $booking_pricing as $key => $value ){
$from = strtotime($value['from']);
$to = strtotime($value['to']) + 86399;
for( $i = 0; $i < $duration; $i++ ){
if( $loop_time >= $from && $loop_time <= $to ){
$cost[] = $value['cost'];
$loop_time += $day;
}
}
}
$cost = array_sum( $cost ) / $duration;
// Output
$product_name .= '<br><span class="booking-details">';
$product_name .= $duration . __(" x Night Price ", "woocommerce") . wc_price($bloc_cost + $cost);
$product_name .= '</span>';
}
return $product_name;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。
我在别墅预订网站上使用 Woocommerce 和 Woocommerce Bookings,但我有一个小问题无法解决。
我想将字段 "Details" 添加到我的购物车 (Cart Details)。此字段将显示预订的持续时间以及每个别墅的价格/晚。
我的别墅有一些具有特定区块成本的资源。
关于持续时间值,我可以使用以下代码显示它:
<?php
/*display_card_data();*/
$items = WC()->cart->get_cart();
foreach($items as $item) {
$duration = $item['booking']['duration'];
}
// displaying values for test
echo $duration. ' x Night Price' .$price ;
?>
我想知道如何在此字段中显示块成本。
请提供任何帮助。
这可以通过以下代码完成(但在 WC Bookings 中它们可以是许多单价):
add_filter( 'woocommerce_cart_item_name', 'booking_details_after_name', 30, 3 );
function booking_details_after_name( $product_name, $cart_item, $cart_item_key ) {
if ( isset( $cart_item['booking']['duration'] ) ){
// Duration
$duration = $cart_item['booking']['duration'];
// Price cost ( they can be many different )
$base_cost = get_post_meta( $cart_item['product_id'], '_wc_booking_cost', true );
$block_cost = get_post_meta( $cart_item['product_id'], '_wc_booking_block_cost', true );
// Output
$product_name .= '<br><span class="booking-details">';
$product_name .= $duration . __(" x Night Price ", "woocommerce") . wc_price($base_cost);
$product_name .= '</span>';
}
return $product_name;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。
具体更新:
add_filter( 'woocommerce_cart_item_name', 'booking_details_after_name', 30, 3 );
function booking_details_after_name( $product_name, $cart_item, $cart_item_key ) {
if ( isset( $cart_item['booking']['duration'] ) ){
// Duration
$duration = (int) $cart_item['booking']['_duration'];
$resource_id = $cart_item['booking']['_resource_id'];
$start_time = $cart_item['booking']['_start_date'];
$end_time = $cart_item['booking']['_end_date'];
$loop_time = (int) $start_time;
$day = 86400; // In seconds
// Price cost ( they can be many different )
$res_block_cost = get_post_meta( $cart_item['product_id'], '_resource_block_costs', true );
$booking_pricing = get_post_meta( $cart_item['product_id'], '_wc_booking_pricing', true );
foreach ( $res_block_cost as $key => $value ){
if( $key == $resource_id ){
$bloc_cost = $value;
break;
}
}
$cost = array();
foreach ( $booking_pricing as $key => $value ){
$from = strtotime($value['from']);
$to = strtotime($value['to']) + 86399;
for( $i = 0; $i < $duration; $i++ ){
if( $loop_time >= $from && $loop_time <= $to ){
$cost[] = $value['cost'];
$loop_time += $day;
}
}
}
$cost = array_sum( $cost ) / $duration;
// Output
$product_name .= '<br><span class="booking-details">';
$product_name .= $duration . __(" x Night Price ", "woocommerce") . wc_price($bloc_cost + $cost);
$product_name .= '</span>';
}
return $product_name;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。