Providing high-quality service requires you to optimize multiple processes in your store, including order management. That being said, looking for certain order details might take some time, especially with hundreds of products.
Nevertheless, Magento allows you to eliminate the hard work. You can get order data by increment ID in Magento in multiple ways, the same as you can get current product or current category.
So, in this article, you'll learn how to use each method.
Post Contents [hide]
Get Order Data by Increment ID Using Factory Method
The factory method is used the most frequently. Use the following code:
protected $orderFactory; public function __construct( \Magento\Sales\Model\OrderFactory $orderFactory ) { $this->orderFactory = $orderFactory; } public function getOrderByIncrementId() { $orderIncrementId = 10000003; $order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId); }
Get Order Data by Increment ID Using API Repository
Here is one more option. You might as well use the API repository to get order by increment ID.
protected $order; public function __construct( \Magento\Sales\Api\Data\OrderInterface $order ) { $this->order = $order; } public function getOrderByIncrementId() { $orderIncrementId = 10000485; $order = $this->order->loadByIncrementId($orderIncrementId); }
Get Order Data by Increment ID Using Object Manager
Object Manager can be used to get order data as well. Even though it seems to be the easiest one, it is, in fact, rather complicated.
$orderIncrementId= '1000101628'; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->get(\Magento\Sales\Api\Data\OrderInterface::class)->loadByIncrementId($orderIncrementId); print_r($order->getData());
Note: it's recommended to avoid the usage of the object manager method since it hides the real dependencies of the class.
So, which method do you like the best?
The above methods of getting order data aren't too complicated. Having learned how to deal with getting order data by increment ID, it is important to learn how to get store information in Magento 2.