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

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.

Magento 2 Speed & Performance Optimization Services

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;
use Magento\Framework\Exception\NoSuchEntityException;

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require __DIR__ . '/../app/bootstrap.php';

$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');

try {
     $state->setAreaCode('frontend');
} catch (\Magento\Framework\Exception\LocalizedException $e) {
     echo "<br><strong style='color:red;'>An error occurred:</strong> " . $e->getMessage();
}

$orderId = 1; //Your Order Id

$orderRepository = $objectManager->create(\Magento\Sales\Api\OrderRepositoryInterface::class);

try {
     $order = $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><br>-----Your Customer Data-----<br>"; 
     echo "<br>Customer First Name : " . $order->getCustomerFirstname(); 
     echo "<br>Customer Last Name : " . $order->getCustomerLastname();

     echo "<br><br>-----Your Billing Address Data-----<br>";
     echo "<pre>";
     print_r($order->getBillingAddress()->getData());
     echo "</pre>";

     echo "<br>-----Your Shipping Address Data-----<br>"; 
     echo "<pre>";
     print_r($order->getShippingAddress()->getData());
     echo "</pre>";

} catch (NoSuchEntityException $e) {
     echo "<br><strong style='color:red;'>Error:</strong> Order with ID   <strong>{$orderId}</strong> does not exist.";
} catch (Exception $e) {
     echo "<br><strong style='color:red;'>An error occurred:</strong> " . $e->getMessage();
}

?>

hyva theme development

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.

Hire Magento Programmer

Happy Coding!

FAQ

What is an order ID in Magento 2?

An order ID is a unique numeric identifier automatically assigned when an order is created in Magento 2. The order ID is used for developers and store owners to programmatically access and manage order data.

What is the difference between order ID and increment ID?

Order ID is an internal database identifier, but the increment ID is the human-readable order number that customers see and also in the admin panel (ex: 000000123).

Where can I check my order ID in the Magento admin panel?

The order ID can be found under Sales > Orders in the Magento Admin dashboard. The order ID is displayed in the first column next to each order.

Previous Article

Routing in ReactJS with Example

Next Article

How to Add Newsletter Subscriber Programmatically in Magento 2?

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨