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

Node.js | HTTP Module

Node.js, known for its asynchronous and event-driven architecture, offers a multitude of built-in modules that…

2 days ago

Google’s August 2024 Core Update has Fully Rolled Out

Google has officially rolled out its much-anticipated August 2024 Core Update on August 15, 2024,…

3 days ago

Invoicing Guidelines for Independent E-Commerce Businesses

In e-commerce, it's important to understand that it's not just about running an online store.…

4 days ago

Building Dynamic Frontend Applications with Laravel and Alpine.js

In modern web development, building dynamic, interactive front-end applications is essential. Laravel, a powerful PHP…

4 days ago

Exploring Laravel 10’s New Query Builder Enhancements

Laravel, known for its elegant syntax and ease of use, has continually refined its core…

5 days ago

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

As we continue to innovate and enhance the Magento 2 ecosystem, August 2024 has been…

6 days ago