在Woocommerce预订中显示购物车项目名称下方的特定预订数据

时间:2018-04-12 08:51:37

标签: php woocommerce cart price woocommerce-bookings

我在别墅预订网站上使用Woocommerce和Woocommerce预订,我有一个小问题我无法解决。

我想添加一个字段"详细信息"进入我的购物车(Cart 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 ;

?>

我想知道如何在这个字段中显示块成本。

请提供任何帮助。

1 个答案:

答案 0 :(得分:1)

可以使用以下代码完成(但它们可以在WC预订中获得很多单价)

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文件中。经过测试和工作。