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.
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:
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.
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; |
Here we fetch order id or entity id where increment id= 000000002 in database.
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; |
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; public function getOrder() |
$orderId = 2; $order = $this->orderRepository->get($orderId); echo $order->getId(); |
Here we fetch order id by id.
$orderId = 2; |
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.
<?php class Test extends \Magento\Framework\App\Action\Action public function __construct( public function execute() |
Here we fetch order id or entity id of quote_id=5.
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.
<?php class Test extends \Magento\Framework\App\Action\Action public function __construct( public function execute() |
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.
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.
Sign In
Create New Account