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.
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.
So here we will learn How 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.
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!
Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…
E-commerce has transformed the way consumers shop for products and services and interact with businesses.…
The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…
Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…
Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…
We have brought exciting news for Magento store owners. Hyvä Themes recently released 1.3.6 and…