:::: MENU ::::
Posts tagged with: order

Magento 2 get order information by increment id

Today we discuss about Magento 2 get order information by increment id.This tutorial include how to change or get information of order by order increment id, how to load order by increment id. There are three ways to do that Api Repository, Factory method and object manager. So let start with our example.

Factory Method:

Best practice method always use this method

namespace QaisarSatti\Module\Block;
class Product extends \Magento\Framework\View\Element\Template
{
 protected $orderFactory;
public function __construct(

        \Magento\Sales\Model\OrderFactory $orderFactory,

    ) {


        $this->orderFactory = $orderFactory;

    }

public function getOrderByIncrementId(){
        $orderIncrementId = 10000003;
        $order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);

}


}

Api Repository:

This was introduce in Magento 2.1 by magento.

namespace QaisarSatti\Module\Block;
use Magento\Catalog\Api\ProductRepositoryInterface;
class Product extends \Magento\Framework\View\Element\Template
 {
     protected $order;
public function __construct(

\Magento\Sales\Api\Data\OrderInterface $order,

    ) {


        $this->order = $order;

    }

public function getOrderByIncrementId(){
        $orderIncrementId = 10000003;
        $order = $this->order->loadByIncrementId($orderIncrementId);

}
}

Object Manager:

Sometime you need to load order record in phtml file. Not recommended method to use in magento2 but so magento use it so here it is just for knowledge.

$orderIncrementId=10000003;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderInfo = $objectManager->create('Magento\Sales\Model\Order')->load($orderIncrementId);
Author: Qaisar Satti
Category: Magento 2
Last Modified: August 17, 2018