How To

Magento 2: How to Get Order Details using REST API

Hello Magento Friends,

Today I will be figuring out, Magento 2: How to Get Order Details using REST API. Due to any circumstances, you failed to check our earlier Magento tutorial, Magento 2: How to Remove the Cancel Button From The Billing Address Edit Form.

Introduction:

By default, Magento 2 manages different types of entities, like products, customers, orders, and so on. Magento manages these all entities while customers purchase products from an E-commerce store. And as a developer sometimes we need all that entity data, and Magento default provides different ways to get that data.

Checkout: How to Create Custom REST API in Magento 2?

So here we will learn How to Get Order Details using REST API in Magento 2.

Steps to Get Order Details using REST API in Magento 2:

Step 1: Create the getOrderDetails.php script file inside your Magento root folder and add the below code

<?php
$userData = array("username" => "admin", "password" => "adminPassword");
$baseUrl = "http://127.0.0.1/magento24/index.php"; // your magento base url
$ch = curl_init($baseUrl."/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

$token = curl_exec($ch);

$ch = curl_init($baseUrl."/rest/V1/orders/1");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

$result = curl_exec($ch);

$result = json_decode($result, 1);
echo '<pre>';print_r($result);?>

Note – You can use this code wherever you want in your Magento file system. E.g: You can also use it in your custom module.

Conclusion:

This way you can get order details using REST API in Magento 2. Apart from this, you can also fetch order status using REST API in Magento 2. If you have any difficulty with the implementation of the above code, specify in the comment part below. Also, share the article amongst your Magento friends and social media. Stay tuned with us for more such solutions.

Happy Coding!

Click to rate this post!
[Total: 20 Average: 4.2]
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

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…

8 hours ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

3 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

3 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

3 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

5 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We have brought exciting news for Magento store owners. Hyvä Themes recently released 1.3.6 and…

6 days ago