【问题标题】:Get the metadata of an order item in woocommerce 3在 woocommerce 3 中获取订单项的元数据
【发布时间】:2018-07-11 22:46:27
【问题描述】:

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

我使用了get_data()get_item(),但是当我尝试使用get_data() 访问字段meta_data 时,它给了我这个错误:

 UNCAUGHT ERROR: CANNOT USE OBJECT OF TYPE WC_DATETIME AS ARRAY IN  

对于get_item()meta_data 的值为 null,因为它是受保护的。

我怎样才能得到这些值?

【问题讨论】:

  • 感谢网络程序员!

标签: php wordpress woocommerce metadata orders


【解决方案1】:

尝试以下方法:

// 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>';
}

相关:

【讨论】:

  • 非常感谢您^^。我测试了这段代码 $item->get_meta_data(); 并运行它。
【解决方案2】:
/* 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>';
    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 2019-03-31
    • 2020-01-25
    相关资源
    最近更新 更多