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 MagentoSalesModelOrderRepository
*/
protected $_orderRepository; /** * @var MagentoFrameworkApiSearchCriteriaBuilder */
protected $_searchCriteriaBuilder; /**
* @param MagentoSalesModelOrderRepository $orderRepository
* @param MagentoFrameworkApiSearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct( MagentoSalesModelOrderRepository $orderRepository, MagentoFrameworkApiSearchCriteriaBuilder $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()!==MagentoCatalogModelProductType::TYPE_SIMPLE && $item->getProductType()!==MagentoCatalogModelProductType::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()!==MagentoCatalogModelProductType::TYPE_SIMPLE && $item->getProductType()!==MagentoCatalogModelProductType::TYPE_VIRTUAL) continue; $orderItemId =$item->getItemId(); $sku = $item->getSku(); $qtyPurchased =$item->getQtyOrdered(); } ..... } }
} /**
* @param string $incrementId
* @return MagentoSalesApiDataOrderInterface $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 MagentoSalesApiDataOrderInterface $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.

Similar Posts