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

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

9 hours ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

6 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

6 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

7 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

1 week ago