Get the metadata of an order item in woocommerce 3

前端 未结 1 1714
不思量自难忘°
不思量自难忘° 2020-12-01 13:12

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\":\"ca         


        
相关标签:
1条回答
  • 2020-12-01 13:57

    Try the following:

    // 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); echo '</pre>';
        echo '<pre>'; print_r($formatted_meta_data); echo '</pre>';
    }
    

    Related:

    • How to get WooCommerce order details
    • Get Order items and WC_Order_Item_Product in WooCommerce 3
    0 讨论(0)
提交回复
热议问题