問題描述
我正在嘗試獲取訂單的行項目.
我正在這樣做:
$order = new WC_Order(147);foreach ($order->get_items() as $key => $lineItem) {print_r('<pre>----');打印_r($lineItem);print_r('----</pre>');}
我可以看到我需要的所有數據,但數組顯示:
[meta_data:protected] =>大批
如何訪問該數組以獲取值?
謝謝.
從 WooCommerce 3.0+ 開始,訂單項目有了新的對象類
WC_Order_Item_Product
.
現在不能像以前一樣直接訪問訂單項屬性
因此,如果您查看輸出的原始數據,您會發現每個行項目現在都是一個對象,并且您將能夠以獨占方式訪問受保護的數據:
WC_Order_Item_Product
getters 方法(或者用 setters 方法改變它)...WC_Order_Item
get_formatted_meta_data( '', true )
方法來訪問所有元數據.它提供了一組可訪問的對象.請參閱WC_Data
方法get_meta()
訪問每個元數據.WC_Data
getters 方法來取消保護此數據并使用以下方法通過數組訪問它:get_data()
(這個方法非常有用)get_meta()
(這個方法最有用)get_data_keys()
get_meta_data()
(不會解除數據保護,使用get_formatted_meta_data()
)
wc_get_order_item_meta()
強大> 專用功能.
WC_Order_Item_Product
getter 方法:
//獲取 WC_Order 對象的實例$order = wc_get_order(147);//遍歷每個訂單項foreach ($order->get_items() as $item_id => $item ) {echo $item->get_type().'
';//訂單項類型echo $item->get_product_id().'
';//產品 IDecho $item->get_variation_id().'
';//變體IDecho $item->get_quantity().'
';//訂單項數量echo $item->get_subtotal().'
';//行項目小計echo $item->get_total().'
';//行項目總計//關聯的產品對象(也不能直接訪問哪些屬性)echo '';print_r( $item->get_product() );echo '</pre>';//... 等等 ...## 測試原始輸出(受保護)//echo '';print_r($item);echo '</pre>';}
wc_get_order_item_meta()
功能.在這里,您可以進入wp_woocommerce_order_itemmeta
表并使用相應的meta_key
(對于line_item
數據類型項 ID):
//獲取 WC_Order 對象的實例$order = wc_get_order(147);//遍歷每個訂單項foreach ($order->get_items() as $item_id => $item ) {echo wc_get_order_item_meta( $item_id, '_product_id', true).'
';//產品編號echo wc_get_order_item_meta( $item_id, '_variation_id', true).'
';//變量 IDecho wc_get_order_item_meta( $item_id, '_qty', true).'
';//數量echo wc_get_order_item_meta( $item_id, '_line_subtotal', true).'
';//行小計//... 等等 ...## 測試原始輸出(受保護的數據)//echo '';print_r($item);echo '</pre>';}
WC_Data
方法 get_data()
方法(取消對數組中數據的保護):
//獲取 WC_Order 對象的實例$order = wc_get_order(147);//遍歷每個訂單項foreach ($order->get_items() as $item_id => $item ) {//在一個可訪問的數組中獲取最有用的 Item 產品數據$item_data = $item->get_data();echo $item_data['id'].'
';//訂單商品IDecho $item_data['order_id'].'
';//訂單編號echo $item_data['product_id'].'
';//產品 IDecho $item_data['variation_id'].'
';//變量 IDecho $item_data['name'].'
';//產品標題(名稱)echo $item_data['quantity'].'
';//訂單項數量echo $item_data['subtotal'].'
';//行項目小計echo $item_data['total'].'
';//行項目總計//... 等等 ...
WC_Data
方法 get_meta()
方法(通過元鍵訪問每個屬性):
//獲取 WC_Order 對象的實例$order = wc_get_order(147);//遍歷每個訂單項foreach ($order->get_items() as $item_id => $item ) {echo $item->get_meta('_product_id').'<br>';//產品 IDecho $item->get_meta('_variation_id').'
';//變量 IDecho $item->get_meta('_qty').'
';//訂單項數量echo $item->get_meta('_line_subtotal').'<br>';//行項目小計echo $item->get_meta('_line_subtotal_tax').'
';//行項目小計稅echo $item->get_meta('_line_total').'<br>';//行項目總計echo $item->get_meta('_line_tax').'<br>';//行項目總稅額//變體的產品屬性echo $item->get_meta('pa_color').'<br>';//顏色echo $item->get_meta('pa_size').'<br>';//顏色//自定義項目元數據echo $item->get_meta('custom_meta_key').'<br>';//自定義元鍵可見//... 等等 ...
相關:如何獲取 WooCommerce 訂單詳情
I am trying to get the line items of an order.
I'm doing this:
$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?
Thanks.
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 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_get_order_item_meta()
dedicated function.
The WC_Order_Item_Product
getters methods:
// 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>';
}
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>';
}
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 ...
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
// ... and so on ...
Related: How to get WooCommerce order details
這篇關于在 Woocommerce 3 中訪問受訂單項保護的數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!