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!

Previous Article

WebP Images - 6 Reasons Why You Need them for Magento 2

Next Article

How to Create Facebook APP -The Step by Step Guide (2024) 

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 ✨