22

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
camelot
  • 301
  • 1
  • 3
  • 12
  • Please describe *when* this action fires - for instance: `woocommerce_checkout_create_order` hook fires before any DB interactions, so order items could not be accessed via $order->get_items(). – Amit Rahav Dec 02 '19 at 11:40

6 Answers6

30

2018 Update:

  • Clarifying the answer with 2 possible cases
  • Added compatibility for woocommerce 3+

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 use get_post_meta() function ( but NOT wc_get_order_item_meta() ).

So here is your code:

global $post;
$order = wc_get_order( $post->ID );
$items = $order->get_items(); 

foreach ( $order->get_items() => $item ) {

    // Compatibility for woocommerce 3+
    $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $item['product_id'] : $item->get_product_id();

    // Here you get your data
    $custom_field = get_post_meta( $product_id, '_tmcartepo_data', true); 

    // To test data output (uncomment the line below)
    // print_r($custom_field);

    // If it is an array of values
    if( is_array( $custom_field ) ){
        echo implode( '<br>', $custom_field ); // one value displayed by line 
    } 
    // just one value (a string)
    else {
        echo $custom_field;
    }
}

2) Get order item meta data (custom field value):

global $post;
$order = wc_get_order( $post->ID );
$items = $order->get_items(); 

foreach ( $order->get_items() as $item_id => $item ) {

    // Here you get your data
    $custom_field = wc_get_order_item_meta( $item_id, '_tmcartepo_data', true ); 

    // To test data output (uncomment the line below)
    // print_r($custom_field);

    // If it is an array of values
    if( is_array( $custom_field ) ){
        echo implode( '<br>', $custom_field ); // one value displayed by line 
    } 
    // just one value (a string)
    else {
        echo $custom_field;
    }
}

If the custom field data is an array, you can access the data in a foreach loop:

// Iterating in an array of keys/values
foreach( $custom_field as $key => $value ){
    echo '<p>key: '.$key.' | value: '.$value.'</p>';
} 

All code is tested and works.

Reference related to data in orders:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi, thanks for your response. However, the $item['product_id'] only could get the order_id and it's not helpful for me. You can see my image above (http://i.stack.imgur.com/8OJ7U.jpg). All I can get is the product ID in meta_value column (number 90), but what I need is the number in order_item_id ( 1 or 2) so I can get the value of _tmcartepo_data meta_key. Any idea? – camelot Sep 09 '16 at 09:06
  • I'm currently get "Call to a member function get_items() on boolean" error for calling get_items() (WP 4.6.1 and WC 2.6.4). Trying to figure out why, but it seems you point me to the right direction. – camelot Sep 09 '16 at 10:41
6

When doing the foreach on $order->get_items(), their key is actually the orderline ID. So:

foreach ( $order->get_items() as $key => $item ) {
    $data = wc_get_order_item_meta( $key, '_tmcartepo_data' );
    ...
}
Berend
  • 2,889
  • 1
  • 18
  • 13
2

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:

$order = wc_get_order( $post->ID );
$items = $order->get_items();


foreach( $items as $item ){
    $data = unserialize($item['item_meta']['_tmcartepo_data'][0]);
    $a = $data[0]['value'];
    $b = $data[1]['value'];
    echo $a;
    echo $b;
}

Tested and works in my case.

jgangso
  • 638
  • 10
  • 19
0

Use this <pre><?php print_r($items); ?></pre> to check all the contents of the $items array/object.

Cagy79
  • 1,610
  • 1
  • 19
  • 25
0
foreach ( $order->get_items() as $key => $item ) {
$data = wc_get_order_item_meta( $key, '_tmcartepo_data' );
...

This solution was worth to me, change "_tmcartepo_data" for your meta_key.

0

A simple way to get order items from database;

/**
 * @param $order_id
 *
 * @return array|null|object
 */
function get_order_items( $order_id ) {
    global $wpdb, $table_prefix;

    $items     = $wpdb->get_results( "SELECT * FROM `{$table_prefix}woocommerce_order_items` WHERE `order_id` = {$order_id}" );
    $item_name = array();

    foreach ( $items as $item ) {
        $item_name[] = $item->order_item_name;
    }

    return $item_name;
}
Mostafa Soufi
  • 645
  • 10
  • 17