【发布时间】:2017-04-04 07:35:24
【问题描述】:
使用 Woocommerce 2.6.8 时,我无法获得 docs 和 here on SO 中所述的订单商品数据信息。
我想要的只是获取 Line Item 的价格和数量,应该很简单:
$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 直到找到我要查找的密钥。
我觉得我必须在这里遗漏一些明显的东西。
【问题讨论】:
标签: php wordpress woocommerce items orders