简体   繁体   中英

WooCommerce Bookings: adding and reordering item data in cart and checkout

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM