I have an API module to get orders with any status for a specific customer.
I am calling the API as :
https://{{magento-store}}/index.php/rest/V1/order/myorder?status=processing
and authoriaztion : bearer token_number in header.
but, I always get an empty array. so please help.
Here is my code.
di.xml into app/code/Ipragmatech/Restfulapi/etc/ :
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="Ipragmatech\Restfulapi\Api\OrderInterface" type="Ipragmatech\Restfulapi\Model\Order" />
</config>
webapi.xml into app/code/Ipragmatech/Restfulapi/etc/ :
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<!-- Routing define -->
<route url="/V1/order/myorder" method="GET">
<service class="Ipragmatech\Restfulapi\Api\OrderInterface" method="myorder"/>
<resources>
<resource ref="self"/>
</resources>
<data>
<parameter name="customerId" force="true">%customer_id%</parameter>
<parameter name="status" force="true">%status%</parameter>
</data>
</route>
</routes>
OrderInterface.php into app/code/Ipragmatech/Restfulapi/Api/ :
<?php
namespace Ipragmatech\Restfulapi\Api;
interface OrderInterface
{
/**
* Return orders.
*
* @param int $customerId
* @param string $status
* @return array
*/
public function myorder($customerId, $status);
}
Order.php into app/code/Ipragmatech/Restfulapi/Model/ :
<?php
namespace Ipragmatech\Restfulapi\Model;
use Ipragmatech\Restfulapi\Api\OrderInterface;
/**
* Defines the implementaiton class of the calculator service contract.
*/
class Order implements OrderInterface
{
public function myorder($customerId, $status) {
if(empty($customerId) || !isset($customerId) || $customerId == "" || empty($status) || !isset($status) || $status == ""){
throw new InputException(__('Id required'));
}
else{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orders = $objectManager->create('Magento\Sales\Model\Order')->getCollection()->addFieldToFilter('status', $status)->addFieldToFilter('customer_id',$customerId);
$orderData = array();
if(count($orders)){
foreach ($orders as $order){
$data = array("order_id"=>$order->getEntityId(),"status"=>$order->getStatus(),
"amount"=>$order->getBaseGrandTotal(),
"order_date"=>$order->getUpdatedAt()
);
$orderData[] = $data;
}
return $orderData;
}
else{
return $orderData;
}
}
}
}
di.xml file into app/code/Ipragmatech/Restfulapi/etc/webapi_rest/ for customer authoriaztion:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Authorization">
<plugin name="customerAuthorization" type="Magento\Customer\Model\Plugin\CustomerAuthorization" />
</type>
<type name="Magento\Authorization\Model\CompositeUserContext">
<arguments>
<argument name="userContexts" xsi:type="array">
<item name="customerSessionUserContext" xsi:type="array">
<item name="type" xsi:type="object">Magento\Customer\Model\Authorization\CustomerSessionUserContext</item>
<item name="sortOrder" xsi:type="string">20</item>
</item>
</argument>
</arguments>
</type>
</config>