Woocommerce - 获取订单项价格和数量。
2022-08-30 16:13:57
使用Woocommerce 2.6.8,我无法获得文档和SO中所述的订单项数据信息。
我想要的只是获得订单项价格和数量,这应该很简单:
$order = new WC_Order( $order_id );
$order_items = $order->get_items();
foreach ($order_items as $items_key => $items_value) {
echo $items_value['name']; //this works
echo $items_value['qty']; //this doesn't work
echo $items_value[item_meta][_qty][0]; //also doesn't work
echo $items_value['line_total']; //this doesn't work
}
仔细观察返回的内容
Array
(
[1] => Array
(
[name] => Sample Product 1
[type] => line_item
[item_meta] =>
[item_meta_array] => Array
(
[1] => stdClass Object
(
[key] => _qty
[value] => 1
)
[2] => stdClass Object
(
[key] => _tax_class
[value] =>
)
[3] => stdClass Object
(
[key] => _product_id
[value] => 8
)
[4] => stdClass Object
(
[key] => _variation_id
[value] => 0
)
[5] => stdClass Object
(
[key] => _line_subtotal
[value] => 50
)
[6] => stdClass Object
(
[key] => _line_total
[value] => 50
)
[7] => stdClass Object
(
[key] => _line_subtotal_tax
[value] => 0
)
[8] => stdClass Object
(
[key] => _line_tax
[value] => 0
)
[9] => stdClass Object
(
[key] => _line_tax_data
[value] => a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}
)
)
)
)
这都是使用记录的Woocommerce方法,为什么我需要的信息存储在其中?item_meta_array
有谁知道我如何获得这些信息?
最好使用记录的方法,而不是粗略地循环通过,直到我找到我正在寻找的密钥。item_meta_array
我觉得我一定错过了这里明显的东西。