How to get order_item_id WooCommerce

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

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 code above gets order_item_id. It usually doesn't work, but it works because I edited get_items in class-wc-order and included:

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

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

Thanks!

+8
wordpress woocommerce
source share
2 answers

It may be too late for your project, but it may be useful for others:

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

The $key variable is the item_id you are looking for.

+21
source share

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

 foreach ($order_items as $order_item_id => $order_item) { } 
0
source share

All Articles