Woocommerce - 获取订单商品的价格和数量. [英] Woocommerce - Getting the order item price and quantity.
问题描述
使用 Woocommerce 2.6.8 时,我无法获取 docs 和 这里是 SO.
我想要的只是获取 Line Item 的价格和数量,这应该很简单:
$order = new WC_Order( $order_id );$order_items = $order->get_items();foreach ($order_items as $items_key => $items_value) {回声 $items_value['name'];//这有效回声 $items_value['qty'];//这不起作用回声 $items_value[item_meta][_qty][0];//也不起作用回声 $items_value['line_total'];//这不起作用}
仔细查看返回的内容
数组([1] =>大批([名称] =>样品产品 1[类型] =>line_item[item_meta] =>[item_meta_array] =>大批([1] =>标准类对象([键] =>_数量[值] =>1)[2] =>标准类对象([键] =>_tax_class[值] =>)[3] =>标准类对象([键] =>_product_id[值] =>8)[4] =>标准类对象([键] =>_variation_id[值] =>0)[5] =>标准类对象([键] =>_line_小计[值] =>50)[6] =>标准类对象([键] =>_line_total[值] =>50)[7] =>标准类对象([键] =>_line_subtotal_tax[值] =>0)[8] =>标准类对象([键] =>_line_tax[值] =>0)[9] =>标准类对象([键] =>_line_tax_data[值] =>a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}))))
这都是使用文档化的 Woocommerce 方法,为什么我需要的信息存储在这个 item_meta_array
中?
有谁知道我如何获得这些信息?
最好使用记录在案的方法,而不是粗略地循环遍历 item_meta_array
直到找到我正在寻找的密钥.
我觉得我一定在这里遗漏了一些明显的东西.
更新(适用于 WooCommerce 3+)
现在对于代码,您可以使用 WC_Order_Item_Product
(和 WC_Product
)方法相反,例如:
## 对于 WooCommerce 3+ ##//从定义的 ORDER ID 中获取 WC_Order 对象的实例$order = wc_get_order( $order_id );//遍历每一行";订单中的项目foreach ($order->get_items() as $item_id => $item ) {//获取对应WC_Product对象的实例$product = $item_data->get_product();$active_price = $product->get_price();//产品有效原始价格$regular_price = $product->get_sale_price();//产品原始销售价格$sale_price = $product->get_regular_price();//产品原始正常价格$product_name = $item->get_name();//获取商品名称(产品名称)$item_quantity = $item->get_quantity();//获取商品数量$item_subtotal = $item->get_subtotal();//获取未打折的商品行总数$item_subto_tax = $item->get_subtotal_tax();//获取未贴现的项目行总税款$item_total = $item->get_total();//获取商品行总折扣$item_total_tax = $item->get_total_tax();//获取项目行总税收折扣$item_taxes = $item->get_taxes();//获取物品税数组$item_tax_class = $item->get_tax_class();//获取物品税类$item_tax_status= $item->get_tax_status();//获取物品的税务状态$item_downloads = $item->get_item_downloads();//获取项目下载//显示这个数据(检查)echo '产品名称:'.$product_name.'|数量:'.$item_quantity.'|项目总数:'.number_format( $item_total, 2 );}
更新:还有以下所有WC_Abstract_Order
方法允许使用各种有趣的选项获取订单商品数据,例如:
//从定义的 ORDER ID 中获取 WC_Order 对象的实例$order = wc_get_order( $order_id );//遍历每一行";订单中的项目foreach ($order->get_items() as $item_id => $item_data) {## 选项:包括或不包括税款$inc_tax = 真;## 选项:在项目级别(或不)舍入$round = false;//未在项目级别四舍五入(true"表示在项目级别四舍五入)$item_cost_excl_disc = $order->get_item_subtotal( $item, $inc_tax, $round );//计算物品成本(未打折) - 对网关有用.$item_cost_incl_disc = $order->get_item_total( $item, $inc_tax, $round );//计算物品成本(折扣) - 对网关有用.$item_tax_cost = $order->get_item_tax( $item, $inc_tax, $round );//获取物品税成本 - 对网关有用.$item_Line_subtotal = $order->get_line_subtotal( $item, $inc_tax, $round );//获取行小计 - 未打折.$item_Line_total = $order->get_line_total( $item, $inc_tax, $round );//获取行总数 - 折扣$item_Line_tax = $order->get_line_tax( $item );//获取行税$form_line_subtotal = $order->get_formatted_line_subtotal( $item, $tax_display = '' )//获取行小计 - 格式化显示.}
感谢@Casper 的评论
也可以是 WC_Data
方法用于以不受保护的数组形式获取订单商品数据或从特定元键获取特定嵌套或自定义元数据值:
//从定义的 ORDER ID 中获取 WC_Order 对象的实例$order = wc_get_order( $order_id );//遍历每一行";订单中的项目foreach ($order->get_items() as $item_id => $item ) {$order_item_data = $item->get_data();//在未受保护的数组中获取 WooCommerce 订单项元数据print_r( $order_item_data );//显示原始数据$item_meta_data = $item->get_meta_data();//获取未受保护数组中的订单项嵌套和自定义元数据print_r( $item_meta_data );//显示原始数据$item_value = $item->get_meta('meta_key');//从 meta_key 中获取特定订单项自定义或嵌套元数据值print_r( $item_value );//显示原始数据(可以是字符串或数组)}
此代码已经过测试且有效.
<块引用>方法 get_item_meta()
已弃用,已被 wc_get_order_item_meta
它不再是一个方法而是一个带有一些参数的函数:
<块引用>
/** 参数汇总
* @param 混合 $item_id* @param 混合 $key* @param bool $single (默认: true)* @return 混合*/
<块引用>
wc_get_order_item_meta( $item_id, $key, $single = true );
<块引用>
woocommerce 的早期版本(从 2.4 到 2.6.x)
您可以使用 get_item_meta()
WC_Abstract_order 方法,获取订单元数据(商品数量和商品总价).
所以你的代码将是:
//获取订单对象$order";$order = wc_get_order( $order_id );//获取订单中的项目$order_items = $order->get_items();//遍历订单中的每一项foreach ($order_items as $item_id => $item) {//获取产品名称$product_name = $item['name'];//获取商品数量$item_quantity = $order->get_item_meta($item_id, '_qty', true);//获取项目行总数$item_total = $order->get_item_meta($item_id, '_line_total', true);//显示这个数据(检查)echo '产品名称:'.$product_name.'|数量:'.$item_quantity.'|项目总数:'.$item_total;}
此代码经过测试且功能齐全.
Using Woocommerce 2.6.8 , I can't get the Order Item Data information as described in the docs and here on SO.
All I want is to get the Line Item price and Quantity, which should be as simple as:
$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
?
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.
I feel like I must be missing something obvious here.
Update (For WooCommerce 3+)
Now for the code you can use WC_Order_Item_Product
(and WC_Product
) methods instead, like:
## 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 ) {
// Get an instance of corresponding the WC_Product object
$product = $item_data->get_product();
$active_price = $product->get_price(); // The product active raw price
$regular_price = $product->get_sale_price(); // The product raw sale price
$sale_price = $product->get_regular_price(); // The product raw regular price
$product_name = $item->get_name(); // Get the item name (product name)
$item_quantity = $item->get_quantity(); // Get the item quantity
$item_subtotal = $item->get_subtotal(); // Get the item line total non discounted
$item_subto_tax = $item->get_subtotal_tax(); // Get the item line total tax non discounted
$item_total = $item->get_total(); // Get the item line total discounted
$item_total_tax = $item->get_total_tax(); // Get the item line total tax discounted
$item_taxes = $item->get_taxes(); // Get the item taxes array
$item_tax_class = $item->get_tax_class(); // Get the item tax class
$item_tax_status= $item->get_tax_status(); // Get the item tax status
$item_downloads = $item->get_item_downloads(); // Get the item downloads
// Displaying this data (to check)
echo 'Product name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. number_format( $item_total, 2 );
}
Update: Also all the following WC_Abstract_Order
methods allow to get order items data with various interesting options like:
// 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) {
## Option: Including or excluding Taxes
$inc_tax = true;
## Option: Round at item level (or not)
$round = false; // Not rounded at item level ("true" for rounding at item level)
$item_cost_excl_disc = $order->get_item_subtotal( $item, $inc_tax, $round ); // Calculate item cost (not discounted) - useful for gateways.
$item_cost_incl_disc = $order->get_item_total( $item, $inc_tax, $round ); // Calculate item cost (discounted) - useful for gateways.
$item_tax_cost = $order->get_item_tax( $item, $inc_tax, $round ); // Get item tax cost - useful for gateways.
$item_Line_subtotal = $order->get_line_subtotal( $item, $inc_tax, $round ); // Get line subtotal - not discounted.
$item_Line_total = $order->get_line_total( $item, $inc_tax, $round ); // Get line total - discounted
$item_Line_tax = $order->get_line_tax( $item ); // Get line tax
$form_line_subtotal = $order->get_formatted_line_subtotal( $item, $tax_display = '' ) // Gets line subtotal - formatted for display.
}
Thanks to @Casper for his comment
Also WC_Data
methods can be used to get order item data as an unprotected array or to get a specific nested or custom meta data value from a specific meta key:
// 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 ) {
$order_item_data = $item->get_data(); // Get WooCommerce order item meta data in an unprotected array
print_r( $order_item_data ); // display raw data
$item_meta_data = $item->get_meta_data(); // Get order item nested and custom meta data in an unprotected array
print_r( $item_meta_data ); // display raw data
$item_value = $item->get_meta('meta_key'); // Get specific order item custom or nested meta data value from a meta_key
print_r( $item_value ); // display raw data (can be a string or an array)
}
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 function with 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 );
Prior versions of woocommerce (from 2.4 to 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).
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) {
// Get the product name
$product_name = $item['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
这篇关于Woocommerce - 获取订单商品的价格和数量.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!