-1

In Woocommerce I am trying to display the results of order item object and to access it:

$product_meta = $item->get_meta_data();
print_r ($product_meta);

This is what I am trying to pull:

enter image description here

EDIT: This is the output that I get using $item->get_formatted_meta_data( '', true ):

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

2 Answers2

4

To get all order item meta data, you will use WC_Order_Item get_formatted_meta_data() method with specific arguments, this way:

// Accessible non protected Order item meta data
$item_meta_data = $item->get_formatted_meta_data( '', true );

// Formatted raw Output
echo '<pre>'; print_r($item_meta_data); echo '</pre>';

To access some order item properties, you can use any WC_Order_Item_Product method like:

$item->get_product(); // Get the WC_Product object

$item->get_product_id(); // Get the Product ID

$item->get_variation_id(); // Get the Variation ID

$item->get_name(); // Get the Product name

$item->get_quantity(); // Get the item quantity 

// and so on …

Then if you need to access a specific "custom" order item data value, you will use WC_Data get_meta() method:

$custom_value = $item->get_meta("_custom_key");

See: Get Order items and WC_Order_Item_Product in Woocommerce 3


Update (displaying your required custom order item meta data)

The data you need can be accessed and displayed this way:

if( $lessons = $item->get_meta('lessons') ) {
    echo '<p>Lessons: '.$lessons.'</p>';
}

if( $tour_guide = $item->get_meta('tour guide') ) {
    echo '<p>Tour Guide: '.$tour_guide.'</p>';
}

I hope that this works now.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi Loic , how would i echo the value of key and the value of `code`$item_meta_data = $item->get_formatted_meta_data( '', true );`code` ? – John Athanasiou Mar 15 '19 at 13:16
  • 'here_the_meta_key1' this is assuming that its a custom meta ?, mine is not custom meta its been added straight to the product meta from the admin order screen, so it doesn't have a name, look at the picture ..... i am missing something, too dazed to figure it out – John Athanasiou Mar 15 '19 at 13:58
  • changed my question to make it more specific if this helps – John Athanasiou Mar 15 '19 at 14:26
  • yes this works, but sorry for not explaining correctly, the admin of the site changes these on the order screen, so they are not always called lessons and tour guide, he names them according to the service he provides and they are always different ........ i think now you fully understand why i couldnt figure this out – John Athanasiou Mar 15 '19 at 14:43
1

All I did was put this wc_display_item_meta( $item );

and that is it , it pulls the info automatically !!!!!!! admin can change those in the edit order screen to anything and they will appear (thanks to @LoicTheAztec for pointing me to the right direction

enter image description here

  • I forgot to mention this function… But if you look to it source code, it uses `get_formatted_meta_data()` method (Now, your question wasn't really clear and very understandable)… – LoicTheAztec Mar 15 '19 at 16:13