3

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

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

如果我的预订与订单相关联,我会使用该功能检索我的数据wc_get_order_item_meta

我希望能够在预订还不是订单时检索我的数据 (例如,简单地添加到购物篮中)

浏览数据库时,我看到信息存储在表中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);
4

1 回答 1

1

您可以使用WC_Cart方法get_cart()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 文件中或任何插件文件中。

此代码经过测试并且可以工作。

一旦找到方法、名称和数据路径,您就可以将其删除(仅用于测试和开发)......</p>

于 2017-04-20T15:49:48.703 回答