12

I need to be able to get order_item_id, the unique value applied to each item in each order. This is what I have so far:

        global $wpdb;
    $order = new WC_Order( $order_id );
    $items = $order->get_items(); 
        foreach ( $items as $item ) {
            $product_id = $item['product_id'];
            $item_id = $item['item_id'];

The last line in the above code gets order_item_id. It normally wouldn't work but it works because I've edited get_items in class-wc-order and included:

        $items[ $item->order_item_id ]['item_id'] = $item->order_item_id;

What I want to know is how can I get order_item_id without having to edit class-wc-order. Is there any easy way?

Thanks!

user2903890
  • 121
  • 1
  • 1
  • 4

2 Answers2

26

This may be already too late for your project but may be useful for others:

foreach ($items as $key => $product ) 

The $key variable is the item_idyou are looking for.

2rs2ts
  • 10,662
  • 10
  • 51
  • 95
user3367143
  • 261
  • 3
  • 4
2

The index/key of the array returned by $order->get_items() is the order_item_id... so try this:

foreach ($order_items as $order_item_id => $order_item) { 
}
OhBeWise
  • 5,350
  • 3
  • 32
  • 60
user3481396
  • 49
  • 1
  • 4
  • This is saying exactly the same as [user3367143's answer](http://stackoverflow.com/a/22200864/327074) already here. The other answer is using the `$items` variable from the question that is created using `$order->get_items()`. – icc97 May 30 '16 at 12:35