2

I want to display the end date of the booking as well as the start date. Now so far I have the following:

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

This allows me to display the end date of a booking. However, this is after any other meta data which isn't what I want. Is there a way that I could either:

1) Change where the end date appears so that it appears after the Start Date and not after all of the other meta data

2) Even better would be to have the start and end date together as the same bit of meta data.

Thanks!

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
DigM
  • 509
  • 1
  • 4
  • 24

1 Answers1

2

Code lightly updated

Yes this is possible but you have to know the item data key for the 'Start Date'… Here I reorder that items data.

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.

If needed, to display the raw data of $item_data argument you can use print_r($item_data); inside your function before the return at the end.

Here is that code:

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

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

The code is tested with other settings and should work for you.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi Loic, Thanks for the help. I seem to be getting a warning though which says the following: Warning: Invalid argument supplied for foreach() in /home/jonath59/public_html/lashis/wp-content/plugins/woocommerce/includes/class-wc-cart.php on line 591 Any idea what could be causing this? – DigM Feb 20 '17 at 18:24
  • Hi Loic, Your edited answer almost worked. I didn't seem to be returning keys from my item_data and it was throwing a warning. Instead I seemed to be getting ['name'] so I changed the if statement to say if( $value['name'] == $item_data_start_date_key ) which then worked. – DigM Feb 20 '17 at 19:54
  • Yes it is. Thanks for your help! – DigM Feb 20 '17 at 19:58