3

With WooCommerce 3+ introducing new API to fetch the order and it's details, a lot of things have changed and many things break as well.

Consider the following code in my plugin:

$order = wc_get_order($order_id);
$id= 27;

var_dump($order->get_item($id));

which gives me bool(false). I have checked the database and the order and the item does exist.

Also

var_dump($order) does return the entire order object with all the items.

So basically, only the function get_item does not seem to work.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Aashay Shah
  • 47
  • 2
  • 10

1 Answers1

0

The only explanation is that the ID you are using is not an item_id with a type "line_item"

I have tried and it works normally as expected using WC_Abstract_Order get_item() method when the item_id is of type "line_item".

To get and check the correct "line_item" Item IDs from a defined Order ID, try:

// define an exiting order ID first
$order_id = 422;

$order = wc_get_order($order_id);

foreach($order->get_items() as $item_id => $item_values){
    $item_ids_array[] = $item_id;
}

var_dump( $item_ids_array ); // will output all item IDs (of type "line_item") for this order

## ==> Then now you can try (to check get_item() method):

foreach( $item_ids_array as $item_id ){
    var_dump( $order->get_item( $item_id ) ); //  Will output each WC_Order_Item_Product Object …
}

This should clarify things.

As reference: How to get WooCommerce order details

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399