Hello Magento Friends,
In today’s blog, we will learn about How to Get Order Data by Order ID Programmatically in Magento 2.
Order management involves fetching order details like order summary, customer information, payment details, billing, and shipping details. You can get order data from the order id.
Related Product – Magento 2 Custom Order Number
Let’s learn How to get order data from Order Id programmatically in Magento 2.
Steps to Get Order Data by Order ID Programmatically in Magento 2:
Step 1: Create a file in your Magento root directory at the below path
magento_root_directory\getorderdata.php
And then add the following code snippet
<?php use Magento\Framework\App\Bootstrap; ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); require __DIR__ . '/../app/bootstrap.php'; $params = $_SERVER; $bootstrap = Bootstrap::create(BP, $params); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); $registry = $objectManager->get('Magento\Framework\Registry'); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $orderId=1; //Your Order Id $order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId); echo "<br>-----Your Order Data-----<br>"; echo "<br> Entity Id : ".$order->getEntityId(); echo "<br> Increment Id : ".$order->getIncrementId(); echo "<br> State : ".$order->getState(); echo "<br> Status : ".$order->getStatus(); echo "<br> StoreId : ".$order->getStoreId(); echo "<br> Subtotal : ".$order->getSubtotal(); echo "<br> GrandTotal : ".$order->getGrandTotal(); echo "<br> TotalQtyOrdered : ".$order->getTotalQtyOrdered(); echo "<br> OrderCurrencyCode : ".$order->getOrderCurrencyCode(); echo "<br>-----Your Customer Data-----<br>"; echo "<br> Customer First Name : ".$order->getCustomerFirstname(); echo "<br> Customer Last Name : ".$order->getCustomerLastname(); echo "<br>-----Your Billing Address Data-----<br>"; echo "<pre>"; print_r($order->getBillingAddress()->getData());echo '<br>'; echo "</pre>"; echo "<br>-----Your Shipping Address Data-----<br>"; echo "<pre>"; print_r($order->getShippingAddress()->getData()); echo "</pre>"; ?>
Conclusion:
Hence, using the above method, you can retrieve order information using the order ID in Magento 2. Alternatively, when you need order information outside Magento, you can Get Order Information Using SOAP API in Magento 2.
If you face any error while implementing the above code, share it with me through the comments. Share the tutorial with your friends, and stay in touch with us to learn more about Magento 2.
Happy Coding!