I'm trying to extract item meta value from Woocommerce's orders by using:
$data = wc_get_order_item_meta( $item, '_tmcartepo_data', true );
However, I can't find a way to get order_item_id as the first parameter (using get_items)
global $woocommerce, $post, $wpdb;
$order = new WC_Order($post->ID);
$items = $order->get_items();
foreach ( $items as $item ) {
$item_id = $item['order_item_id']; //???
$data = wc_get_order_item_meta( $item_id, '_tmcartepo_data', true );
$a = $data[0]['value'];
$b = $data[1]['value'];
echo $a;
echo $b;
}
And I mean this order item_id (1 and 2)
Order_item_id in database - Image
How can I don that?
Thanks.
A simple way to get order items from database;
Late to the party, but being working with the same point with TM Extra Product Options plugin, I think this is what answers your question:
Tested and works in my case.
This solution was worth to me, change "_tmcartepo_data" for your meta_key.
When doing the foreach on
$order->get_items()
, their key is actually the orderline ID. So:So There can be 2 cases:
1) Get product meta data (not set in order item meta data):
You will need to get the product ID in the foreach loop for a
WC_Order
and to get some metadata for this product you wil useget_post_meta()
function ( but NOTwc_get_order_item_meta()
).So here is your code:
2) Get order item meta data (custom field value):
If the custom field data is an array, you can access the data in a foreach loop:
All code is tested and works.
Reference related to data in orders:
Use this
<pre><?php print_r($items); ?></pre>
to check all the contents of the $items array/object.