phpwoocommercecartwoocommerce-bookings

Display specific booking data below cart item name in Woocommerce Bookings


I'm using Woocommerce and Woocommerce Bookings for a villa booking website and I have a small issue I can't fix.

I'd like to add a field "Details" into my cart (Cart Details). This field will display the duration of the booking and also the price / night of each villa.

Cart Details

My villas have some resources with a specific block cost.

Regarding the duration value, I can display it by using this code bellow :

<?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 ;

?>

I'm wondering how I can display the block cost in this field.

Please any help will be useful.


Solution

  • This can be done with the following code (but they can be many unit prices in 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;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and work.


    Specific Update:

    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;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and work.