How-to-get-order-item-collection-by-item-id-magento-2

How to Get Order Item Collection by Item ID in Magento 2?

In an eCommerce store, in order to maintain the inventory, you might require to get order item collection by item ID in Magento 2. The below-mentioned code can help you get order item collection by item ID in Magento 2.

Create Block file and use this code:

<?php
namespace MageDelight\Training\Block;
class Item extends \Magento\Framework\View\Element\Template
{
 public function __construct(
 \Magento\Framework\View\Element\Template $context,
 \Magento\Sales\Api\OrderItemRepositoryInterface $orderItemRepository,
 array $data = []
 ) {
 $this->orderItemRepository = $orderItemRepository;
 parent::__construct($context, $data);
 }
/* get order Item collection */
 public function getOrderItem($itemIid)
 {
 $itemCollection = $this->orderItemRepository->get($itemId);
 return $itemCollection;
 }
}

Now, Call function from Template file

$itemId = 10; // order item id
$getItemCollection = $block->getOrderItem($itemId);
echo $getItemCollection->getOrderId();
echo "<pre>";print_r($getItemCollection->debug());

We hope we covered everything related to get order item collection by item id in Magento 2. If you need our professional help with Magento Development, feel free to reach out.

Tags