WooCommerce 预订:在创建订单之前检索预订数据

WooCommerce Bookings: Retrieve the booking data before order is created

我正在使用 WooCommerce 预订插件,我目前希望在预订摘要(产品选项)中显示其他信息。

为此,我使用了以下钩子:woocommerce_admin_booking_data_after_booking_details

如果我的预订与订单相关联,我会使用函数 wc_get_order_item_meta

检索我的数据

我希望能够在预订尚未下单时检索我的数据 (例如简单地添加到购物车).

浏览数据库时我看到信息存储在 table woocommerce_sessions.

在我使用的挂钩中,我只能访问预订的 ID。

是否可以从这个中检索到相应的会话?

谢谢

更新

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

您可以使用 WC_Cart method get_cart() or get_cart_from_session() 访问此数据。

您应该通过以下两种方式使用 foreach 循环:

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

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

您可以在此挂钩函数中使用仅检索正确的数据路径和名称(显示将发生在购物车页面中,例如此处):

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

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

此代码已经过测试并有效。

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