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

https://www.drupal.org/project/views_field_view

Rest Export Nested

https://www.drupal.org/project/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.

[{"title":"Drupal Commerce Hoodie - Green, Large","order_item_id":"2","quantity":"6.00","title_1":"Drupal Commerce Hoodie - Green, Large","total_price__number":"$312.00","total_price__currency_code":"USD","unit_price__number":"$52.00","unit_price__currency_code":"USD"}]

 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.

[{"order_number":"","order_items_target_id":"9, 10, 11","total_paid__number":"","total_price__number":"$129.98","total_price__currency_code":"USD","state":"Draft","view":[{"title":"Canvas Backpack","order_item_id":"9","quantity":"1.00","title_1":"Canvas Backpack","total_price__number":"$64.99","total_price__currency_code":"USD","unit_price__number":"$64.99","unit_price__currency_code":"USD"},{"title":"Gold Rim Sunnies","order_item_id":"10","quantity":"1.00","title_1":"Gold Rim Sunnies","total_price__number":"$14.99","total_price__currency_code":"USD","unit_price__number":"$14.99","unit_price__currency_code":"USD"},{"title":"36" x 24" Fawkestrooper Print","order_item_id":"11","quantity":"1.00","title_1":"36" x 24" Fawkestrooper Print","total_price__number":"$50.00","total_price__currency_code":"USD","unit_price__number":"$50.00","unit_price__currency_code":"USD"}]},{"order_number":"","order_items_target_id":2,"total_paid__number":"","total_price__number":"$312.00","total_price__currency_code":"USD","state":"Draft","view":[{"title":"Drupal Commerce Hoodie - Green, Large","order_item_id":"2","quantity":"6.00","title_1":"Drupal Commerce Hoodie - Green, Large","total_price__number":"$312.00","total_price__currency_code":"USD","unit_price__number":"$52.00","unit_price__currency_code":"USD"}]},{"order_number":3,"order_items_target_id":4,"total_paid__number":"","total_price__number":"$25.00","total_price__currency_code":"USD","state":"Draft","view":[{"title":"Knit Hat in Midnight Plum","order_item_id":"4","quantity":"1.00","title_1":"Knit Hat in Midnight Plum","total_price__number":"$25.00","total_price__currency_code":"USD","unit_price__number":"$25.00","unit_price__currency_code":"USD"}]},{"order_number":4,"order_items_target_id":5,"total_paid__number":"$25.00","total_price__number":"$25.00","total_price__currency_code":"USD","state":"Completed","view":[{"title":"Knit Hat in Midnight Plum","order_item_id":"5","quantity":"1.00","title_1":"Knit Hat in Midnight Plum","total_price__number":"$25.00","total_price__currency_code":"USD","unit_price__number":"$25.00","unit_price__currency_code":"USD"}]},{"order_number":5,"order_items_target_id":6,"total_paid__number":"","total_price__number":"$25.00","total_price__currency_code":"USD","state":"Draft","view":[{"title":"Knit Hat in Midnight Plum","order_item_id":"6","quantity":"1.00","title_1":"Knit Hat in Midnight Plum","total_price__number":"$25.00","total_price__currency_code":"USD","unit_price__number":"$25.00","unit_price__currency_code":"USD"}]},{"order_number":6,"order_items_target_id":7,"total_paid__number":"$25.00","total_price__number":"$25.00","total_price__currency_code":"USD","state":"Completed","view":[{"title":"Knit Hat in Midnight Plum","order_item_id":"7","quantity":"1.00","title_1":"Knit Hat in Midnight Plum","total_price__number":"$25.00","total_price__currency_code":"USD","unit_price__number":"$25.00","unit_price__currency_code":"USD"}]},{"order_number":7,"order_items_target_id":"12, 13","total_paid__number":"","total_price__number":"$77.00","total_price__currency_code":"USD","state":"Completed","view":[{"title":"Drupal Commerce Hoodie - Blue, Medium","order_item_id":"12","quantity":"1.00","title_1":"Drupal Commerce Hoodie - Blue, Medium","total_price__number":"$52.00","total_price__currency_code":"USD","unit_price__number":"$52.00","unit_price__currency_code":"USD"},{"title":"Makeup Brush Set","order_item_id":"13","quantity":"1.00","title_1":"Makeup Brush Set","total_price__number":"$25.00","total_price__currency_code":"USD","unit_price__number":"$25.00","unit_price__currency_code":"USD"}]}]

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.

http://your-project/order-details-nested/{user-id}

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...