php Woocommerce - 获取订单商品的价格和数量。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40711160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Woocommerce - Getting the order item price and quantity.
提问by robobobobo
Using Woocommerce 2.6.8 , I can't get the Order Item Data information as described in the docsand here on SO.
使用 Woocommerce 2.6.8 ,我无法获得文档中和SO 上所述的订单项数据信息。
All I want is to get the Line Item price and Quantity, which should be as simple as:
我想要的只是获取 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
}
Looking closer at what gets returned returned
仔细查看返回的内容
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:{}}
)
)
)
)
This is all using documented Woocommerce methods, why is the information I need stored in this item_meta_array
?
这都是使用文档化的 Woocommerce 方法,为什么我需要的信息存储在此item_meta_array
?
Does anyone know how I can get that information?
有谁知道我如何获得这些信息?
Preferably using documented methods as opposed to a crude hack of looping through the item_meta_array
until I find the key I'm looking for.
最好使用记录在案的方法,而不是粗略地循环遍历item_meta_array
直到找到我正在寻找的密钥。
I feel like I must be missing something obvious here.
我觉得我一定在这里遗漏了一些明显的东西。
回答by LoicTheAztec
Update (For WooCommerce 3+)
更新(适用于 WooCommerce 3+)
Now for the code you can use WC_Order_Item_Product
(and WC_Product
) methods instead, like:
现在对于代码,您可以改用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 );
}
This code is tested and works.
此代码经过测试并有效。
Method
get_item_meta()
is deprecated and has been replaced bywc_get_order_item_meta
and it's not anymore a method but a functionwith some parameters:/** 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 );
方法
get_item_meta()
已被弃用并已被替换wc_get_order_item_meta
,它不再是一个方法,而是一个带有一些参数的函数:/** 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 );
Prior versions of woocommerce (from 2.4 to 2.6.x)
woocommerce 的先前版本(从 2.4 到 2.6.x)
You can use get_item_meta()
WC_Abstract_order method, to get the order metadata (the item quantity and the item price total).
您可以使用get_item_meta()
WC_Abstract_order 方法,获取订单元数据(商品数量和商品总价)。
So your code will be:
所以你的代码将是:
// 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;
}
This code is tested and fully functional.
此代码经过测试且功能齐全。
Reference: Class WC_Abstract_Order Methods
回答by Nishad Up
Item price can get from order
object by below code
物品价格可以order
通过以下代码从对象中获取
$order->get_item_total( $item );
回答by Hemel
Please see this documentation for woocommerce Line item in order class. Here
请参阅此文档以了解订单类中的 woocommerce 订单项。 这里
You can call total for get the total order cost. If you want to retrieve the single item cost by taking the product_id
您可以致电 total 获取总订单成本。如果您想通过获取product_id来检索单品成本
$_product = wc_get_product( $product_id );
$Price = $_product->get_price();
Or you can do this.
或者你可以这样做。
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$price = get_post_meta( get_the_ID(), '_sale_price', true);