We are using the Magento 2 REST API. We are able to access the the orders using the admin token.
(http://localhost/index.php/rest/V1/integration/admin/token)
But we are unable to access the orders using consumer.
http://localhost/index.php/rest/V1/integration/customer/token)
Below is the API which I am using:
Get Orders : /rest/V1/orders/items But I am getting the error that Consumer is not authorized to access %resources even I have passed the consumer key in my header.
Is the customer orders REST api is implemented ?
Basically we need to create/use an API which returns Orders by customer.
Does anyone has an idea?
Thanks in advance!!!
Hello Deecee2000,
Magento2 doesn't have default REST APIs for order history of the customer. You have to create your own. There is the extension in Magento Marketplace for order API for customer order items.
There is the following extension :
Thanks for your reply.
What I did was, I used consumer token and based on that I got customer_id in Model Class.
Then I retrieved customer email and then I got orders for that customer by email. It is working as expected now.
Other issue is, I am trying to created customer address based on customer API. I am passing customer address info as POST and try to get customer email/id in Model function, so based on that I add that address to that customer and return newly created address id.
How do I achieve this?
Thanks,
Is there is a solution for this problem with a free extension or custom code ?
You need to write your custom code. Feel free to ask if there is any issue.
Please follow these steps to create an extension to get the multiple orders and single order using REST API
1. Create a module directory with vendor as
app/code/vendor name/module name
2. Create registration file as app/code/vendor name/module name/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor Name_Module name',
__DIR__
);
3. Create an etc directory
app/code/vendor name/module name/etc
4. Create a module.xml file as
app/code/vendor name/module name/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor Name_Module name" setup_version="1.0.0" />
</config>
5.
Create a webapi.xml file as
app/code/vendor name/module name/etc/webapi.xml
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/orders/mine/:id" method="GET">
<service class="Magento\Sales\Api\OrderRepositoryInterface" method="get"/>
<resources>
<resource ref="self" />
</resources>
</route>
<route url="/V1/orders/mine" method="GET">
<service class="Magento\Sales\Api\OrderRepositoryInterface" method="getList"/>
<resources>
<resource ref="self" />
</resources>
</route>
</routes>
6. Now you can get the orders using following REST API
/V1/orders/mine
/V1/orders/mine/:id => :id=order_id
7. It is working for me.
/V1/orders/mine this api is not working,it show response {"message":"%fieldName is a required field.","parameters":{"fieldName":"searchCriteria"}}.If i will send V1/orders/mine/?searchCriteria
is show this response {"message":"No such entity with %fieldName = %fieldValue","parameters":{"fieldName":"orderId","fieldValue":"107"}}.What is issue in this api?
@Jitendra Sharma wrote:Please follow these steps to create an extension to get the multiple orders and single order using REST API
1. Create a module directory with vendor as
app/code/vendor name/module name
2. Create registration file as app/code/vendor name/module name/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor Name_Module name',
__DIR__
);
3. Create an etc directory
app/code/vendor name/module name/etc
4. Create a module.xml file as
app/code/vendor name/module name/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor Name_Module name" setup_version="1.0.0" />
</config>
5.
Create a webapi.xml file as
app/code/vendor name/module name/etc/webapi.xml
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/orders/mine/:id" method="GET">
<service class="Magento\Sales\Api\OrderRepositoryInterface" method="get"/>
<resources>
<resource ref="self" />
</resources>
</route>
<route url="/V1/orders/mine" method="GET">
<service class="Magento\Sales\Api\OrderRepositoryInterface" method="getList"/>
<resources>
<resource ref="self" />
</resources>
</route>
</routes>
6. Now you can get the orders using following REST API
/V1/orders/mine
/V1/orders/mine/:id => :id=order_id
7. It is working for me.
Try to send such query:
/rest/V1/orders/mine?searchCriteria=[]
http://www.yourulr.com/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=customer_email&searchCriteria[filter_groups][0][filters][0][value]=test@gmail.com
by using the REST API you can get the list of orders placed by the user replace user email in place of test@gmail.com
I'm a bit late to the party, but for anyone else stuck with this..
Change your XML for the route too:
<route url="/V1/orders/mine" method="GET">
<service class="Magento\Sales\Api\OrderRepositoryInterface" method="getList"/>
<resources>
<resource ref="self" />
</resources>
<data>
<parameter name="searchCriteria.filter_groups.0.filters.9999999.value" force="true">%customer_id%</parameter>
<parameter name="searchCriteria.filter_groups.0.filters.9999999.field" force="true">customer_id</parameter>
</data>
</route>
This will force the current customer id as a filter in the search criteria