在WooCommerce中按用户ID获取购物车详细信息

时间:2019-05-05 14:03:37

标签: php wordpress session woocommerce cart

我正在尝试在后端显示所有注册用户的购物车详细信息。

1 个答案:

答案 0 :(得分:0)

要通过用户ID (例如用于后端使用)获取购物车商品详细信息,是从WooCommerce会话中获取的,例如在本工作示例(用于已定义的用户ID)中:< / p>

// The User ID
$user_id = 123;

// Get an instance of the WC_Session_Handler Object
$session_handler = new WC_Session_Handler();

// Get the user session from its user ID:
$session = $session_handler->get_session($user_id);

// Get cart items array
$cart_items = maybe_unserialize($session['cart']);

// Loop through cart items and get cart items details
foreach( $cart_items as $cart_item_key => $cart_item ) {
    $product_id   = $cart_item['product_id'];
    $variation_id = $cart_item['variation_id'];
    $quantity     = $cart_item['quantity'];
    $attributes   = $cart_item['variation'];
    $item_taxes   = $cart_item['line_tax_data'];
    $subtotal_tax = $cart_item['line_subtotal_tax'];
    $total_tax    = $cart_item['line_tax'];
    $subtotal     = $cart_item['line_subtotal'];
    $total        = $cart_item['line_total'];
}