在 woocommerce 3 中获取订单项的元数据

Get the metadata of an order item in woocommerce 3

如何获取产品 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。字段 IDname 我可以访问。

我使用了 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>';
}

相关:

/* Get Order Meta in Array[] format at thank you page - woocommerce */

add_action( 'woocommerce_thankyou', 'get_order_meta_at_thankyoupage', 20, 1);
function get_order_meta_at_thankyoupage( $order_id ){
  $orderr = wc_get_order($order_id);
 echo '<pre>';
 print_r($orderr);
 echo '</pre>';
    
}