0

I am currently using the

\Magento\Sales\Model\OrderFactory $_orderFactory

and

loadByIncrementId($id)

in my custom block and calling it in my .phtml file.

I get basic details of the order, but I have only single item in the order, still I have to use the $bookingDetail->getAllItems() in foreach loop. Is it really necessary? Can't we get a single item detail without the foreach loop? Also about single Product option: getproductOptions() loop for single option/value?

What is the best practice?

3

There is no need to use order factory to fetch a particular Order item.

That case, you can use order item repository class Magento\Sales\Api\OrderItemRepositoryInterface

That has get() and getList() method using these you can fetch a particular item data.

If you have an order item id then you can use get() and that is the best method in that case.

But if have an order id then you can use getList()` and using Search Criteria , you can an order items order.

You can follow below article for how you can filter a Magento 2 Object Repository

Filtering a Magento 2 Object Repository

Magento2: How to add multiple AND and OR conditions to filter/filterGroups?

Same Code:

<?php


namespace StackExchange\Magento\Model;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Sales\Api\OrderItemRepositoryInterface;

class OrderItem {

    /**
     * @var \Magento\Framework\Api\SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    /**
     * @var \Magento\Framework\Api\Search\FilterGroupBuilder
     */
    private $filterGroupBuilder;

    /**
     * @var \Magento\Framework\Api\FilterBuilder
     */
    private $filterBuilder;

    /**
     * @var \Magento\Sales\Api\OrderItemRepositoryInterface
     */
    private $orderItemRepository;

    public function __construct(
        OrderItemRepositoryInterface $orderItemRepository,
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
        \Magento\Framework\Api\FilterBuilder $filterBuilder,
        \Magento\Framework\Api\Search\FilterGroupBuilder $filterGroupBuilder     
    ) {

        $this->orderItemRepository = $orderItemRepository;
        $this->filterBuilder = $filterBuilder;
        $this->filterGroupBuilder = $filterGroupBuilder;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }
    public function getOrderItemByOrderItemId()
    {
     $id = 99;
     try{
         $orderItem = $this->orderItemRepository->get($id);
         return $orderItem;
     } catch (NoSuchEntityException $ex) {
         return false;
     }

     return false;
    }
    public function getOrderItemByOrderId()
    {
        $orderId = 15;
        $productId = 105;

        $searchCriteria = $this->searchCriteriaBuilder->addFilter('order_id', $orderId)
                                    ->addFilter('product_id', $productId);
        $orderItems = $this->orderItemRepository->getList($searchCriteria);
        if($orderItems->getTotalCount() > 0)
        {
            $orderItemsData = $orderItems->getItems();
            // Running foreach as item collection in array
            foreach($orderItemsData as $orderItem)
            {
                // 
                $orderItem;
            }
        }

    }
}
  • Calling API method form with in magento is also not recommended as API methods are for connecting other systems to magento. So hence its not a best practice. – M Shoaib Qureshi Feb 6 at 8:15
  • 1
    @MShoaibQureshi OrderItemRepositoryInterface methods are not "API-Methods". they are just exposed by the API. but used within Magento itself. so yes, this is indeed the best-practice – Philipp Sander Feb 6 at 8:25
  • where you have found that API is not recommended a process at Magento 2? Magento 2 suggest to use service contact for fetch data and they are deprecating the Model class and it load() function. If you will checkout Magento inner code then will find out. See more magento.stackexchange.com/questions/158081/… – Amit Bera Feb 6 at 8:26
  • Repository Interface class is an important part of service contact, In future, the Fetch data using Model will be mostly deprecated. There will be some minor section, the model will be used for backward compatibility – Amit Bera Feb 6 at 8:30
  • @AmitBera [link(]magento.stackexchange.com/a/155341) Here it mentions that "Also, don't use the API from within Magento like Khoa is suggesting, it does not make sense. The API is for connecting Magento with other systems." – M Shoaib Qureshi Feb 6 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.