在Woocommerce 3中访问受订单商品保护的数据 [英] Accessing Order Items protected data in Woocommerce 3
问题描述
我正在尝试获取订单的订单项.
I am trying to get the line items of an order.
我正在这样做:
$order = new WC_Order(147);
foreach ($order->get_items() as $key => $lineItem) {
print_r('<pre>----');
print_r($lineItem);
print_r('----</pre>');
}
我可以看到我需要的所有数据,但数组显示如下:
I can see all the data I need but the array shows this:
[meta_data:protected] => Array
如何访问此数组以获取值?
How can I access this array to get the values?
谢谢.
推荐答案
自WooCommerce 3.0+用于订单商品以来,有了新的Object类
WC_Order_Item_Product
.
现在无法像以前一样直接访问订单商品属性
Since WooCommerce 3.0+ for Order items there is new Object class
WC_Order_Item_Product
.
Now Order items properties can't be accessed directly as before
因此,如果查看输出的原始数据,您将看到每个订单项现在都是一个对象,并且可以使用以下方式独家访问受保护的数据:
So if you look at your output raw data you will see that each line item is now an object, and you will be able to access that protected data using exclusively:
-
WC_Order_Item_Product
getters方法(或使用setters方法进行更改)… -
WC_Order_Item
get_formatted_meta_data( '', true )
方法来访问所有元数据.它提供了一组可访问的对象.请参见WC_Data
方法get_meta()
访问每个元数据. -
WC_Data
吸气剂方法取消保护此数据并使用以下方法通过数组访问它:-
get_data()
(此方法非常有用) -
get_meta()
(此方法最有用) -
get_data_keys()
-
get_meta_data()
(不会取消保护数据,请使用get_formatted_meta_data()
)
-
WC_Order_Item_Product
getters methods (or to change it with the setters methods)…WC_Order_Item
get_formatted_meta_data( '', true )
method to access all meta data. It gives an array of accessible objects. SeeWC_Data
methodget_meta()
to access each meta data.WC_Data
getters methods to unprotect this data and access it through arrays using methods:get_data()
(this method is the very useful)get_meta()
(this method is the most useful)get_data_keys()
get_meta_data()
(does not unprotect the data, useget_formatted_meta_data()
)
WC_Order_Item_Product
getter方法:
// Get an instance of the WC_Order object
$order = wc_get_order(147);
// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {
echo $item->get_type().'<br>'; // The order item type
echo $item->get_product_id().'<br>'; // The Product ID
echo $item->get_variation_id().'<br>'; // The variation ID
echo $item->get_quantity().'<br>'; // Line item quantity
echo $item->get_subtotal().'<br>'; // Line item subtotal
echo $item->get_total().'<br>'; // Line item total
// The associated product object (which properties can't be accessed directly too)
echo '<pre>'; print_r( $item->get_product() ); echo '</pre>';
// ... and so on ...
## Testing raw output (protected)
// echo '<pre>'; print_r($item); echo '</pre>';
}
wc_get_order_item_meta()
函数.在这里,您可以进入 wp_woocommerce_order_itemmeta
表,并使用相应的 meta_key
(对于line_item
数据类型项目ID)输出项目ID的所有数据:>
The wc_get_order_item_meta()
function. Here you can go in wp_woocommerce_order_itemmeta
table and output any data for an item ID using the corresponding meta_key
(for line_item
data type item ID):
// Get an instance of the WC_Order object
$order = wc_get_order(147);
// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {
echo wc_get_order_item_meta( $item_id, '_product_id', true). '<br>'; // Product ID
echo wc_get_order_item_meta( $item_id, '_variation_id', true). '<br>'; // Variation ID
echo wc_get_order_item_meta( $item_id, '_qty', true). '<br>'; // quantity
echo wc_get_order_item_meta( $item_id, '_line_subtotal', true). '<br>'; // Line subtotal
// ... and so on ...
## Testing raw output (protected data)
// echo '<pre>'; print_r($item); echo '</pre>';
}
WC_Data
方法get_data()
方法(取消保护数组中的数据):
The WC_Data
method get_data()
method (to unprotect the data in an array):
// Get an instance of the WC_Order object
$order = wc_get_order(147);
// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {
// Get the most useful Item product data in an accessible array
$item_data = $item->get_data();
echo $item_data['id'].'<br>'; // The order item ID
echo $item_data['order_id'].'<br>'; // The order ID
echo $item_data['product_id'].'<br>'; // The Product ID
echo $item_data['variation_id'].'<br>'; // The Variation ID
echo $item_data['name'].'<br>'; // The Product title (name)
echo $item_data['quantity'].'<br>'; // Line item quantity
echo $item_data['subtotal'].'<br>'; // Line item subtotal
echo $item_data['total'].'<br>'; // Line item total
// ... and so on ...
WC_Data
方法get_meta()
方法(通过其元键访问每个属性):
The WC_Data
method get_meta()
method (to access each property by its meta key):
// Get an instance of the WC_Order object
$order = wc_get_order(147);
// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {
echo $item->get_meta('_product_id').'<br>'; // The Product ID
echo $item->get_meta('_variation_id').'<br>'; // The Variation ID
echo $item->get_meta('_qty').'<br>'; // Line item quantity
echo $item->get_meta('_line_subtotal').'<br>'; // Line item subtotal
echo $item->get_meta('_line_subtotal_tax').'<br>'; // Line item subtotal tax
echo $item->get_meta('_line_total').'<br>'; // Line item total
echo $item->get_meta('_line_tax').'<br>'; // Line item total tax
// Product attributes for variation
echo $item->get_meta('pa_color').'<br>'; // Color
echo $item->get_meta('pa_size').'<br>'; // Color
// Custom item meta gata
echo $item->get_meta('custom_meta_key').'<br>'; // custom meta key visible
echo $item->get_meta('_custom_meta_key').'<br>'; // custom meta key not visible
// ... and so on ...
这篇关于在Woocommerce 3中访问受订单商品保护的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!