How To

How to Get Order Data by Order ID Programmatically in Magento 2

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!

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

24 hours ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

1 day ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 days ago

How to Choose the Best Shopify Development Company?

In today’s digital landscape, e-commerce has become a cornerstone for businesses looking to thrive. Among…

2 days ago

Flutter | Card Widget

Flutter is a popular UI toolkit that allows developers to create beautiful, natively compiled applications…

3 days ago

Resolving 403 Forbidden Errors in Chrome

The 403 Forbidden error is an everyday issue users may face while browsing the internet.…

3 days ago