Disclaimer: This is a user generated content submitted by a member of the WriteUpCafe Community. The views and writings here reflect that of the author and not of WriteUpCafe. If you have any complaints regarding this post kindly report it to us.

Magento 2 has a lot more to offer than Magento 1.

If you have an eCommerce site running on Magento 2 platform then knowing how to get item data from your online store is a thing you should know about. This post is going to teach you how to retrieve this.

Item data or product data collection in Magento 2 means showing you the number of items present in your store.

The below code describes how to use item id to get order item collection in Magento 2. Let’s fetch the products in your Magento 2 store.

<?php
declare(strict_types=1);

namespace MageSparkOrderItemBlock;

use MagentoFrameworkViewElementTemplate;
use MagentoSalesApiDataOrderItemInterface;
use MagentoSalesApiOrderItemRepositoryInterface;

/**
 * Class Item
 *
 * @package MageSparkOrderItemBlock
 */
class Item extends Template
{
    /**
     * @var OrderItemRepositoryInterface
     */
    protected $orderItemRepository;

    /**
     * Item constructor.
     * @param Template $context
     * @param OrderItemRepositoryInterface $orderItemRepository
     * @param array $data
     */
    public function __construct(
        Template $context,
        OrderItemRepositoryInterface $orderItemRepository,
        array $data = []
    ) {
        $this->orderItemRepository = $orderItemRepository;
        parent::__construct($context, $data);
    }

    /**
     * Get order Item collection
     *
     * @param $itemId
     * @return OrderItemInterface
     */
    public function getOrderItem($itemId): OrderItemInterface
    {
        return $this->orderItemRepository->get($itemId);
    }
}

For More Information, Visit:- https://www.magespark.com/blog/post/how-to-get-order-item-collection-by-item-id-in-magento-2?utm_source=writeupcafe&utm_medium=article&utm_campaign=august-2022

https://www.magespark.com/
Do you like Magespark's articles? Follow on social!