3

I am using WooCommerce Bookings plugin and I currently looking to display additional information in the booking summary (product options).

To do this I use the following hook: woocommerce_admin_booking_data_after_booking_details

If my reservation is linked to an order, I retrieve my data using the function wc_get_order_item_meta

I would like to be able to retrieve my data when the reservation is not yet an order (simply added to the basket for example).

When browsing the database I saw that the information was stored in the table woocommerce_sessions.

In the hook I use, I only have access to the ID of the reservation.

Is it possible to retrieve the corresponding session from this one?

Thanks

UPDATE

add_filter('woocommerce_admin_booking_data_after_booking_details', function ($booking_id) {
global $wpdb;
$booking = get_wc_booking($booking_id);
$order = $booking->get_order();
if ($order) {
    foreach ($order->get_items() as $item) {
        $item_meta = wc_get_order_item_meta($item->get_id(), '', FALSE);
        /* Your code */
    }
} else {
    $table = $wpdb->prefix . 'woocommerce_sessions';
    $condition = '%booking_id____' . $booking_id . '%';
    $sql = "SELECT session_value FROM $table WHERE session_value LIKE '$condition'";
    $query = maybe_unserialize($wpdb->get_var($sql));
    $cart_items = maybe_unserialize($query['cart']);
    foreach ($cart_items as $item) {
        /* Your code */
    }
}
}, 10, 1);
Antoine
  • 33
  • 1
  • 5

1 Answers1

1

You can access to this data with the WC_Cart method get_cart() or get_cart_from_session().

You should use a foreach loop this 2 ways:

foreach(WC()->cart->get_cart() as $cart_item_key => $item_values){
    // Outputting the raw Cart items data to retrieve Bookings related data
    echo '<pre>'; print_r($item_values);  echo '</pre>';
}

Or

foreach(WC()->cart->get_cart() as $cart_item_key => $item_values){
    // Outputting the raw Cart items data to retrieve Bookings related data
    echo '<pre>'; print_r($item_values);  echo '</pre>';
}

You can use just retrieve the correct data path and names in this hooked function (the display will happen in cart page for example here):

add_action( 'woocommerce_before_cart_table', 'my_custom_cart_items_raw_output');
function my_custom_cart_items_raw_output() {
    foreach(WC()->cart->get_cart() as $cart_item_key => $item_values){
        // Outputting the raw Cart items data to retrieve Bookings related data
        echo '<pre>'; print_r($item_values);  echo '</pre>';
    }
}

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

This code is tested and works.

Once found the ways, names and data path, you can remove it (is just for tests and development)…

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you for your help. However I want to display the data in the admin panel and I do not have access to the cart but only to the reservation ID. – Antoine Apr 20 '17 at 15:56
  • @Antoine This is going to be much more complicated, as the only way is to get the data from sessions… So you have to get all cart sessions data first, then to process this data in multiple foreach loops to get the `reservetion_id` and then to associate to each `reservetion_id` the correct data you want… all this in a custom function…You can use $wpdb to get the data in an SQL query. But this is going to be quiet complicated and you will not be able to get always a defined user. – LoicTheAztec Apr 20 '17 at 16:02
  • Okay I see. I have to go through a custom query to retrieve all cart sessions or there is a native function? – Antoine Apr 20 '17 at 16:05
  • @Antoine Humm… I don't think that there is anything native for that. the way is SQL query and time. **But remember that you will not always be able to get a defined user.** User will be defined if they are logged in. – LoicTheAztec Apr 20 '17 at 16:06
  • 1
    Looking at the content in the table this will actually turn out to be complicated ... A product can be added to the shopping cart by a user who is not logged in. Thank you for your time :) – Antoine Apr 20 '17 at 16:29