Drupal Commerce – How to create Rest API for user order details and user order items.

Here we are going to discuss  how to create rest APIs for user order details and user order item details.

Here we are going to achieve this without writing code. We will be using views rest export  and Rest export nested contributed module.

Read this article for configuring required modules.  https://www.digitalnadeem.com/drupal/drupal-commerce-rest-api-configuration-and-creating-api-for-product-categories/

Make sure you have below modules installed.

View Field view


Rest Export Nested


Here we have create one rest API which will provide orders details, inside order details object of all order items details.

In order to achieve this, first we are creating a Rest export view of order items  which can be filtered using order item id’s.

First create a  Rest export view of order item as below.

In view settings select Order item in show select option and type as all. Also check check box option Provide a rest export.

Select show:fields and add below order item fields.

Select Context filter as Order item ID.

In context filter configuration select allow multiple values.

Here you can preview result by applying order item id.

So result will as below with All fields.

 This api we are not using as such. We will be attaching this view to Order details API.

Order details API 

First create a rest export view of order as below.

Then Again Add Rest export nested display to this rest export view. This is need for attaching Order item details view as a field. Add below fields.

You can see Global:view field , this is order item details view, we have added as field in this rest export display.

You can add this view in add fields -> Select view from the list.

In view field configuration you have to select below options.

Select view and display as above. Also provide {{ fields.order_items_target_id }} in contextual filter, you will get this in Replacement patterns. 

Note: If you are seeing any error  while saving the view field configuration, you can save the order details view and configure again.

Now you can see each order details attached corresponding order item details.

So json response will be as below.

Filter order item by user id

While implementing User order API, we need orders details of a particular user. So for this first add User relationship to the Order details View nested rest export display as below. Then add User ID as context filter.

So here we can pass user in API url and get order details of that particular user.

You can add user id in fields and confirm from which user id order  is placed.


Allow below permissions if you are using anonymous API URL. That is without any authentication if you are API end point URL – http://your-project/order-details-nested/2, here 2 is the user ID.

recommended usage is with any of the authentication method like cookie authentication or basic authentication with user name and password. Make sure this user has above mentioned permissions checked.

Get Free E-book
Get a free Ebook on Drupal 8 -theme tutorial
I agree to have my personal information transfered to MailChimp ( more information )
if you like this article Buy me a Coffee this will be an ispiration for me to write articles like this.

You may also like...