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);
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)…