Magento 2 : Get order information by increment or order id

Magento 2 uses Service Contracts for retrieving and saving data from the database. In Magento 2, the concept is called repositories and we have repository classes for all entities like order, product, category etc. which commonly uses get(), getList() and save() methods.

This keeps developers away from calling model classes and methods directly by using methods like load() or save() or loadByIncrementId(), these methods are being deprecated and Magento 2 promotes to use the Service Contracts.

Even if you look at Magento 2 API implementation, it also heavily utilises repository classes for retrieving data.

Anyways let’s crack on with getting order information using increment or order id in Magento 2 -:

<?php

/**
* @var \Magento\Sales\Model\OrderRepository
*/
protected $_orderRepository;

/**
 * @var \Magento\Framework\Api\SearchCriteriaBuilder
 */
protected $_searchCriteriaBuilder;

/**
* @param \Magento\Sales\Model\OrderRepository $orderRepository
* @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(
  \Magento\Sales\Model\OrderRepository $orderRepository,
  \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
)
{
  $this->_orderRepository = $orderRepository;
  $this->_searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
* @param int $orderId
* @return $this
*/
public getOrderInformationByEntityId($orderId)
{
  $order = $this->getOrderById($orderId);
  $orderId = $order->getEntityId();
  $orderGrandTotal = $order->getGrandTotal();
  
  foreach ($order->getAllItems() as $item) {
      if ($item->getProductType()!==\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
          && $item->getProductType()!==\Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL) continue;
          $orderItemId  =$item->getItemId();
          $sku = $item->getSku();
          $qtyPurchased =$item->getQtyOrdered();
  }
  .....
}

/**
* @param string $incrementId
* @return $this
*/
public getOrderInformationByIncrementId($incrementId)
{
  $orders = $this->getOrderByIncrementId($incrementId);
  if (!empty($orders)) {
      foreach ($orders as $order) {
          $orderId = $order->getEntityId();
          $orderGrandTotal = $order->getGrandTotal();
          
          foreach ($order->getAllItems() as $item) {
              if ($item->getProductType()!==\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
                  && $item->getProductType()!==\Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL) continue;
                  $orderItemId  =$item->getItemId();
                  $sku = $item->getSku();
                  $qtyPurchased =$item->getQtyOrdered();
          }
          .....
      }
  }
}

/**
* @param string $incrementId
* @return \Magento\Sales\Api\Data\OrderInterface $order
*/
public function getOrderByIncrementId($incrementId) {
  $this->_searchCriteriaBuilder->addFilter('increment_id', $incrementId);

  $order = $this->_orderRepository->getList(
      $this->_searchCriteriaBuilder->create()
  );

  return $order->getItems();
}

/**
* @param int $id
* @return \Magento\Sales\Api\Data\OrderInterface $order
*/
public function getOrderById($id) {
    return $this->orderRepository->get($id);
}

........
........

There are two main functions get() and getList() which helps to get order information in Magento 2 by using order or increment id.

Also check out how are we retrieving data using searchCriteriaBuilder class using filters. You can add any sort of filters or conditions to get data in Magento 2, very similar to collection classes in Magento 1. More information on filter and condition types can be found using the below post Magento 2 : Condition Type In Search Filter

That’s it, Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.