获取woocommerce 3中订单项的元数据 [英] Get the metadata of an order item in woocommerce 3
本文介绍了获取woocommerce 3中订单项的元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取产品woocommerce的元数据? 我的产品具有现场定制功能,我需要获取此数据.
how to get metadata of a product woocommerce? I have field custom en my products and I need to get this data.
{"ID":151,
"ORDER_ID":251,
"NAME":"car",
"PRODUCT_ID":87,
"VARIATION_ID":0,
"QUANTITY":1,
"TAX_CLASS":"",
"SUBTOTAL":"3",
"SUBTOTAL_TAX":"0",
"TOTAL":"3",
"TOTAL_TAX":"0",
"TAXES":{"TOTAL":[],
"SUBTOTAL":[]},
"META_DATA":[{"ID":1433,
"KEY":"my_car",
"VALUE":"red"}]}
但是总是结果是一样的,我无法访问字段meta_data
.我可以访问ID
和name
字段.
But the always result is the same, I can't access to field meta_data
. The field ID
and name
I have access.
我使用了get_data()
和get_item()
,但是当我尝试使用get_data()
访问字段meta_data
时,出现此错误:
I used get_data()
and get_item()
, but when I try access with get_data()
to field meta_data
it give me this error:
UNCAUGHT ERROR: CANNOT USE OBJECT OF TYPE WC_DATETIME AS ARRAY IN
对于get_item()
,由于受保护,值meta_data
为空.
And with get_item()
, the value meta_data
is null because is protected.
我如何获得这些值?
推荐答案
尝试以下操作:
// Get the $order object from an ID (if needed only)
$order = wc_get_order( $order_id);
// Loop through order line items
foreach( $order->get_items() as $item ){
// get order item data (in an unprotected array)
$item_data = $item->get_data();
// get order item meta data (in an unprotected array)
$item_meta_data = $item->get_meta_data();
// get only additional meta data (formatted in an unprotected array)
$formatted_meta_data = $item->get_formatted_meta_data();
// Display the raw outputs (for testing)
echo '<pre>'; print_r($item_meta_data); echo '</pre>';
echo '<pre>'; print_r($formatted_meta_data); echo '</pre>';
}
相关:
- How to get WooCommerce order details
- Get Order items and WC_Order_Item_Product in Woocommerce 3
这篇关于获取woocommerce 3中订单项的元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文