Get Order API: Magento 2 Get Customer Order And Details Using API

I am creating a Magento 2 customer REST API module to get customer orders API using this web API route URL( get order API ).

So in order to get the order details first, we need to get the admin access token then we can add that tokens in the API header then we have to call order API with passing that AccessToken.

Get Order API in Magento 2

 

Create Custom Customer Orders API

I want to show here to get customer orders using Magento 2 REST API. This custom module will help you to get the customer's order in Magento 2.

In order to create a custom Magento 2 extension for customer REST  API, you have to create some basic files.

Here, I am creating a Model, Controller, and API component of the customer REST API.

app/code/Wishusucess/GetOrders/registration.php

app/code/Wishusucess/GetOrders/etc/module.xml

app/code/Wishusucess/GetOrders/etc/di.xml

app/code/Wishusucess/GetOrders/etc/webapi.xml

app/code/Wishusucess/GetOrders/frontend/routes.xml

app/code/Wishusucess/GetOrders/Model/Api/Custom.php

app/code/Wishusucess/GetOrders/Controller/Index/Index.php

app/code/Wishusucess/GetOrders/Api/CustomInterface.php

 

Step 1: Registration of Getting Order API

app/code/Wishusucess/GetOrders/registration.php

<?php
/**
* Developer: Hemant Singh Magento 2x Developer
* Category: Wishusucess_GetOrders 
* Website: http://www.wishusucess.com/
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Wishusucess_GetOrders',
__DIR__
);

 

Step 2: Create Module XML File

app/code/Wishusucess/GetOrders/etc/module.xml

<?xml version="1.0"?>
<!--
/**
* Developer: Hemant Singh Magento 2x Developer
* Category: Wishusucess_GetOrders 
* Website: http://www.wishusucess.com/
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Wishusucess_GetOrders" setup_version="1.1.4" />
</config>

 

Step 3: Define Dependency Injection

app/code/Wishusucess/GetOrders/etc/di.xml

<?xml version="1.0"?>
<!--
/**
* Developer: Hemant Singh Magento 2x Developer
* Category: Wishusucess_GetOrders 
* Website: http://www.wishusucess.com/
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Wishusucess\GetOrders\Api\CustomInterface" type="Wishusucess\GetOrders\Model\Api\Custom"/>
</config>

 

Step 4: Create Web API XML for Route URL

Now, we have to decide the method and route URL to get access to the other websites. The method will decide to perform the operation over the API request and the URL will decide /V1/wishusucess/getorders/ how we can get access the details.

app/code/Wishusucess/GetOrders/etc/webapi.xml

<?xml version="1.0"?>
<!--
/**
* Developer: Hemant Singh Magento 2x Developer
* Category: Wishusucess_GetOrders 
* Website: http://www.wishusucess.com/
*/
-->
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
<route method="GET" url="/V1/wishusucess/getorders/">
<service class="Wishusucess\GetOrders\Api\CustomInterface" method="getPost"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>

 

Step 5: Route ID

app/code/Wishusucess/GetOrders/frontend/routes.xml

<?xml version="1.0"?>
<!--
/**
* Developer: Hemant Singh Magento 2x Developer
* Category: Wishusucess_GetOrders 
* Website: http://www.wishusucess.com/
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="standard">
<route id="wishusucess" frontName="wishusucess">
<module name="Wishusucess_GetOrders" />
</route>
</router>
</config>

 

Step 6: Custom API Model for Getting Order

app/code/Wishusucess/GetOrders/Model/Api/Custom.php

<?php
/**
* Developer: Hemant Singh Magento 2x Developer
* Category: Wishusucess_GetOrders 
* Website: http://www.wishusucess.com/
*/
namespace Wishusucess\GetOrders\Model\Api;

use Psr\Log\LoggerInterface;

class Custom 
{
/** @var \Magento\Framework\View\Result\PageFactory */
protected $resultPageFactory;
protected $orderRepository;


public function __construct(
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
array $data = []
) {
$this->resultPageFactory = $resultPageFactory;
$this->orderRepository = $orderRepository;

}

/**
* @inheritdoc
*/

public function getPost($orderid)
{
$response = ['success' => false];

try {
$order = $this->orderRepository->get($orderid);
$object['order_info'] = $order->getData();
$object['payment_info'] =$order->getPayment()->getData();
$object['shipping_info'] =$order->getShippingAddress()->getData();
$object['billing_info'] =$order->getBillingAddress()->getData();
$resul=array();
foreach ($order->getAllItems() as $item)
{
//fetch whole item information
$resul= $item->getData();

}
$object['items'] = $resul;

// $response = json_decode(json_encode($object), true);
$response = $object;

} catch (\Exception $e) {
$response = ['success' => false, 'message' => $e->getMessage()];
}
$returnArray = $response;
return $returnArray; 
}
}

 

Step 7: Index Controller in Custermer Orders API

app/code/Wishusucess/GetOrders/Controller/Index/Index.php

<?php
/**
* Developer: Hemant Singh Magento 2x Developer
* Category: Wishusucess_GetOrders 
* Website: http://www.wishusucess.com/
*/
namespace Wishusucess\GetOrders\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action {

/** @var \Magento\Framework\View\Result\PageFactory */
protected $resultPageFactory;
protected $_orderCollectionFactory;
protected $orderRepository;
protected $data;


public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
array $data = []
) {
$this->orderRepository = $orderRepository;
$this->data = $data;

parent::__construct($context);
}

public function execute() {
echo "<pre>";
$orderid = 12; // its called increment id
$order = $this->orderRepository->get($orderid);
$object['order_info'] = $order->getData();
$object['payment_info'] =$order->getPayment()->getData();
$object['shipping_info'] =$order->getShippingAddress()->getData();
$object['billing_info'] =$order->getBillingAddress()->getData();
$object['incrementid'] =$order->getIncrementId();
$object['grandtotal'] =$order->getGrandTotal();
$object['subtotal'] =$order->getSubtotal();
$object['customerid'] =$order->getCustomerId();
$object['customeremail'] =$order->getCustomerEmail();
$object['customerfirstname'] =$order->getCustomerFirstname();
$object['customerlastname'] =$order->getCustomerLastname();

print_r($object)

// print_r(json_decode(json_encode($object), true));

die();

}

}

 

Step 8: Create Custom Interface Customer Orders API

app/code/Wishusucess/GetOrders/Api/CustomInterface.php

<?php
/**
* Developer: Hemant Singh Magento 2x Developer
* Category: Wishusucess_GetOrders 
* Website: http://www.wishusucess.com/
*/
namespace Wishusucess\GetOrders\Api;

interface CustomInterface
{
/**
* GET for Post api
* @return boolean|array
* @param string $orderid order id.
*/

public function getPost($orderid);
}

 

Now run the below commands:

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento setup:static-content:deploy -f

php bin/magento c:c

 

Now I am showing you the testing result by using this custom REST API of a customer order in Magento 2. I am showing you the result by using the postman tool to check the valid web API URL.

First, we have to create an admin token then we will pass that admin token in the header of the order rest api.

Our customer rest API endpoint (/rest/V1/wishusucess/getorders) has to call on the postman tools.

You have to remember that we can not call our rest API without using the rest API endpoint on any other server.

Magento 2 customer Get Order API

 

Download API

Related Post:

Magento 2 Top Developers In USA: Develop Your E-Commerce Websites

File Permissions: Magento 2 Ownership And File Permission At Installation

Shipping Address API: Customer Shipping Details Using API in Magento 2

 

Recommended Post:

PayPal Account: How to Create New PayPal Account, Transfer

Magento 2.4 Installation Guide: How to Install Magento 2.4.2