Magento 2 API – How to Get Customer Order History

magento2ordersrest api

Get list of all previous orders of the customer using Customer token in API.

Best Answer

Solution 1

You can make your custom API for the same;

Using filter Builder you can use search criteria

$customerId = $this->userContext->getUserId();

$filter = $this->filterBuilder
               ->setField('customer_id')
               ->setValue($customerId)
               ->setConditionType('eq')
               ->create();
$this->searchCriteriaBuilder->addFilters([$filter]);

$searchCriteria = $this->searchCriteriaBuilder->create();

Solution 2

Magento is using resource Magento_Sales::sales because of this we can't access it using Customer's token so for the same we need to override Specific API with

<resource ref="self" />

And using this we can access all orders of specific customer using customer's Token in Magento API.

<route url="/V1/orders" method="GET">
        <service class="Magento\Sales\Api\OrderRepositoryInterface" method="getList"/>
        <resources>
            <resource ref="self" />
        </resources>
    </route>
Related Topic