如何获取产品 woocommerce 的元数据? 我的产品有现场定制,我需要获取此数据。
{"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
。
我使用了 get_data()
和 get_item()
,但是当我尝试使用 get_data()
访问字段 meta_data
它给我这个错误:
UNCAUGHT ERROR: CANNOT USE OBJECT OF TYPE WC_DATETIME AS ARRAY IN
并且使用 get_item()
,值 meta_data
为空,因为它受到保护。
我怎样才能得到这些值?
最佳答案
尝试以下操作:
// 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 All item meta data even hidden (in an unprotected array)
$formatted_meta_data = $item->get_formatted_meta_data( '_', true );
// Display the raw outputs (for testing)
echo '<pre>' . print_r($item_meta_data, true) . '</pre>';
echo '<pre>' . print_r($formatted_meta_data, true) . '</pre>';
}
相关:
关于php - 在 woocommerce 3 中获取订单项的元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48565643/