How to Load Order By Id in Magento 2 Programmatically
This Blog help to understand how we load order by ID, load order by increment ID, load order by quote ID, load quote by customer ID in Magento 2.
Load Order by Increment id in Magento2:
To load order by increment id,there are three possible ways: 1)Object Method, 2)Factory Method and 3)API Repository Method.
Let’s See these Methods:
Object Method:
We can use the order interface Magento\Sales\Api\Data\OrderInterface to load order by increment id.
<?php namespace Mageants\Blog\Controller\Index; class Test extends \Magento\Framework\App\Action\Action { protected $orderFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Sales\Api\Data\OrderInterface $orderinterface ) { $this->orderinterface = $orderinterface; return parent::__construct($context); } public function execute() { $incrementId = "000000030"; $order = $this->orderinterface->loadByIncrementId($incrementId); echo $order->getId(); exit; } } |
Using $order->getId() We fetch the order id or entity id.
Factory Method:
To load order id or entity id from Increment Id using factory method,First of all inject in your class an instance of \Magento\Sales\Model\OrderFactory.We create $orderFactory object for this class.
Namespace Mageants\Blog\Controller\Index;class Test extends \Magento\Framework\View\Element\Template{protected $orderFactory;public function __construct(\Magento\Sales\Model\OrderFactory $orderFactory,) {$this->orderFactory = $orderFactory;}public function getOrderByIncrementId(){$orderIncrementId = 000000002;$order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);}} |
Here we fetch order id or entity id where increment id= 000000002 in database.
Api Repository:
To load order id or entity id from Increment Id using Api Repository method,we can use Magento\Catalog\Api\ProductRepositoryInterface instance and create it’s object($order).
namespace Mageants\Blog\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 = 000000002;$order = $this->order->loadByIncrementId($orderIncrementId);}} |
Magento 2 load order by id:
Api Repository:
To load order from Id using Api Repository method,we can use Magento\Catalog\Api\ProductRepositoryInterface instance and cretae it’s object($order).
namespace Mageants\Blog\Controller\Index;use Magento\Catalog\Api\ProductRepositoryInterface;class Order extends \Magento\Framework\View\Element\Template{protected $order;public function __construct(\Magento\Sales\Api\Data\OrderInterface $order,) {$this->order = $order;} public function getOrder(){$orderId = $this->getRequest()->getParam('order_id');$order = $this->order->load($orderId);}} |
$orderId = 2; $order = $this->orderRepository->get($orderId); echo $order->getId(); |
Here we fetch order id by id.
Object Manager:
$orderId = 2;$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId); echo $order->getId(); |
Magento 2 load order by Quote id:
To load order id or entity id from quote id,First of all inject in your class an instance of \Magento\Sales\Model\ResourceModel\Sale\Collection.We create $salesorder object for this class.
<?phpnamespace Mageants\Blog\Controller\Index; class Test extends \Magento\Framework\App\Action\Action{ public function __construct(\Magento\Framework\App\Action\Context $context,\Magento\Sales\Model\ResourceModel\Sale\Collection $salesorder){$this->salesorder = $salesorder;return parent::__construct($context);} public function execute(){$loadorder=$this->salesorder->addFieldToFilter('quote_id',5);print_r($loadorder->getData());}} |
Here we fetch order id or entity id of quote_id=5.
Magento 2 load Quote by Customer id
To load quote id from customer id,First of all inject in your class an instance of \Magento\Sales\Model\ResourceModel\Sale\Collection.We create $salesorder object for this class.
<?phpnamespace Mageants\Blog\Controller\Index; class Test extends \Magento\Framework\App\Action\Action{ public function __construct(\Magento\Framework\App\Action\Context $context,\Magento\Sales\Model\ResourceModel\Sale\Collection $salesorder){$this->salesorder = $salesorder;return parent::__construct($context);} public function execute(){$loadorder=$this->salesorder->addFieldToFilter('customer_id',1);print_r($loadorder->getData());}} |
Here we fetch quote id from customer id where customer_id=1.
If you want to understand about How to load customer by id and by email in Magento 2 then, you can read our blog by click Here.
Conclusion:
We hope above blog helps you to clearly understand How To Load Order By Id Magento 2 Programmatically.
In case of any kind of problem with the above code implementation, you can contact us or let us know in comment section.