rob*_*obo 18 php wordpress items orders woocommerce
使用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
}
Run Code Online (Sandbox Code Playgroud)
仔细观察返回的内容
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:{}}
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
这都是使用文档化的Woocommerce方法,为什么我需要存储的信息item_meta_array
呢?
有谁知道我怎么能得到这些信息?
最好使用文档化的方法,而不是粗略的循环,item_meta_array
直到找到我正在寻找的密钥.
我觉得我必须在这里遗漏一些明显的东西.
Loi*_*tec 48
更新(适用于WooCommerce 3+)
现在对于代码,您可以使用WC_Order_Item_Product
(和WC_Product
)方法,例如:
## For WooCommerce 3+ ##
// Getting an instance of the WC_Order object from a defined ORDER ID
$order = wc_get_order( $order_id );
// Iterating through each "line" items in the order
foreach ($order->get_items() as $item_id => $item_data) {
// Get an instance of corresponding the WC_Product object
$product = $item_data->get_product();
$product_name = $product->get_name(); // Get the product name
$item_quantity = $item_data->get_quantity(); // Get the item quantity
$item_total = $item_data->get_total(); // Get the item line total
// Displaying this data (to check)
echo 'Product name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. number_format( $item_total, 2 );
}
Run Code Online (Sandbox Code Playgroud)
此代码经过测试和运行.
方法
get_item_meta()
已被弃用并已被替换为wc_get_order_item_meta
,它不再是方法,而是具有一些参数的函数:Run Code Online (Sandbox Code Playgroud)/** Parameters summary * @param mixed $item_id * @param mixed $key * @param bool $single (default: true) * @return mixed */ wc_get_order_item_meta( $item_id, $key, $single = true );
早期版本的woocommerce(从2.4到2.6.x)
您可以使用get_item_meta()
WC_Abstract_order方法获取订单元数据(项目数量和项目总价).
所以你的代码将是:
// Getting the order object "$order"
$order = wc_get_order( $order_id );
// Getting the items in the order
$order_items = $order->get_items();
// Iterating through each item in the order
foreach ($order_items as $item_id => $item_data) {
// Get the product name
$product_name = $item_data['name'];
// Get the item quantity
$item_quantity = $order->get_item_meta($item_id, '_qty', true);
// Get the item line total
$item_total = $order->get_item_meta($item_id, '_line_total', true);
// Displaying this data (to check)
echo 'Product name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. $item_total;
}
Run Code Online (Sandbox Code Playgroud)
此代码经过测试且功能齐全.