How to enable Rest API endpoints for cart operations in Drupal commerce using commerce cart api module
Here we are going to discuss exposing API end points for cart operation in Drupal commerce. So we have contributed module for this. Install below contributed in your Drupal commerce instance.
Commerce cart API https://www.drupal.org/project/commerce_cart_api
After installation you can see below resources enabled in Rest UI screen.
/admin/config/services/rest
Here we are enabling basic auth authentication to all resources what we are using here.
Install commerce cart api module , you can see below resources enabled in Rest web services
- Add to cart
- Get Cart
- Updating Cart
- Remove item from Cart
We are exposing above rest API response in below steps.
Add to Cart
URL – http://youproject.com/cart/add?_format=json
Headers: ‘Content-Type: application/json’
Request Method: POST
Request Body : –
[{ “purchased_entity_type”: “commerce_product_variation”, “purchased_entity_id”: “6”, “quantity”: “1”, “combine”: true }]
Here purchased entity id is product variation id what you are receiving in product details response in below rest response. refer article Drupal Commerce Rest API – list Products and Product details
you can see How to get Product details API . See this article for getting response for product details.
Here combine = true increment the order item quantity in cart and combine=false will create a new order item.
See the below response for add to cart API.
[{“order_item_id”:2,”uuid”:”6d882c0b-fd96-4b8d-ad4c-6237ce6d2e2d”,”order_id”:2,”purchased_entity”:{“variation_id”:6,”uuid”:”bea5ef34-3be5-4f8c-9473-500166cd90eb”,”type”:”clothing”,”product_id”:1,”sku”:”DCHOODIE-GRE-LG”,”title”:”Drupal Commerce Hoodie – Green, Large”,”list_price”:{“number”:null,”currency_code”:null,”formatted”:null},”price”:{“number”:”52.000000″,”currency_code”:”USD”,”formatted”:”$52.00″},”attribute_color”:2,”attribute_size”:7,”field_images”:[4,5,6],”weight”:{“number”:”1.000000″,”unit”:”kg”}},”title”:”Drupal Commerce Hoodie – Green, Large”,”quantity”:”1″,”unit_price”:{“number”:”52.000000″,”currency_code”:”USD”,”formatted”:”$52.00″},”total_price”:{“number”:”156.00″,”currency_code”:”USD”,”formatted”:”$156.00″}},{“order_item_id”:3,”uuid”:”60338509-574d-40b1-8ac8-c92912129d4b”,”order_id”:2,”purchased_entity”:{“variation_id”:10,”uuid”:”d0bcaa47-dd2e-4938-978b-e9852ed2c846″,”type”:”simple”,”product_id”:5,”sku”:”Favorit-VintageTypewriter”,”title”:”Vintage Typewriter”,”list_price”:{“number”:null,”currency_code”:null,”formatted”:null},”price”:{“number”:”149.990000″,”currency_code”:”USD”,”formatted”:”$149.99″},”field_images”:[10],”weight”:{“number”:”1.000000″,”unit”:”kg”}},”title”:”Vintage Typewriter”,”quantity”:”1″,”unit_price”:{“number”:”149.990000″,”currency_code”:”USD”,”formatted”:”$149.99″},”total_price”:{“number”:”299.98″,”currency_code”:”USD”,”formatted”:”$299.98″}}]
Here order_id is the id that represents the id of the cart and order_item_id is the identification each item in the cart.
If you resubmit this add to cart request , it will update quantity of same products.
So in this response you can see order_item_id in above cart response, we will use this for updating cart quantity.
Get cart
Here we are getting all carts and items in the cart.
URL– http://youproject.com/cart?_format=json endpoint will return a collection of the current user’s carts.
Request method: Get
Headers: ‘Content-Type: application/json’
Here we will get below response.
[{“order_id”:2,”uuid”:”156b0640-110b-4406-a7c9-297f9afc0398″,”order_number”:null,”store_id”:1,”order_items”:[{“order_item_id”:2,”uuid”:”6d882c0b-fd96-4b8d-ad4c-6237ce6d2e2d”,”order_id”:2,”purchased_entity”:{“variation_id”:6,”uuid”:”bea5ef34-3be5-4f8c-9473-500166cd90eb”,”type”:”clothing”,”product_id”:1,”sku”:”DCHOODIE-GRE-LG”,”title”:”Drupal Commerce Hoodie – Green, Large”,”list_price”:{“number”:null,”currency_code”:null,”formatted”:null},”price”:{“number”:”52.000000″,”currency_code”:”USD”,”formatted”:”$52.00″},”attribute_color”:2,”attribute_size”:7,”field_images”:[4,5,6],”weight”:{“number”:”1.000000″,”unit”:”kg”}},”title”:”Drupal Commerce Hoodie – Green, Large”,”quantity”:”4.00″,”unit_price”:{“number”:”52.000000″,”currency_code”:”USD”,”formatted”:”$52.00″},”total_price”:{“number”:”208.00″,”currency_code”:”USD”,”formatted”:”$208.00″}},{“order_item_id”:3,”uuid”:”60338509-574d-40b1-8ac8-c92912129d4b”,”order_id”:2,”purchased_entity”:{“variation_id”:10,”uuid”:”d0bcaa47-dd2e-4938-978b-e9852ed2c846″,”type”:”simple”,”product_id”:5,”sku”:”Favorit-VintageTypewriter”,”title”:”Vintage Typewriter”,”list_price”:{“number”:null,”currency_code”:null,”formatted”:null},”price”:{“number”:”149.990000″,”currency_code”:”USD”,”formatted”:”$149.99″},”field_images”:[10],”weight”:{“number”:”1.000000″,”unit”:”kg”}},”title”:”Vintage Typewriter”,”quantity”:”3.00″,”unit_price”:{“number”:”149.990000″,”currency_code”:”USD”,”formatted”:”$149.99″},”total_price”:{“number”:”449.97″,”currency_code”:”USD”,”formatted”:”$449.97″}}],”total_price”:{“number”:”657.97″,”currency_code”:”USD”,”formatted”:”$657.97″},”coupons”:[]}]
Here you can see order_id for cart and cart items under order_items.
Updating cart quantity
URL – http://yourproject.com/cart/2/items?_format=json
Request type: PATCH
Header : Content-Type: application/json
Request Body: {“2”:{“quantity”:”6″},”3″:{“quantity”:”6″}}
Here we are updating cart.
In URL, 2 means order_id. Request body is quantity of order_items.
So response will be as provided below. Both the order items updated to quantity 6, based on quantity total price also will be updated.
{ “order_id”: 2, “uuid”: “156b0640-110b-4406-a7c9-297f9afc0398”, “order_number”: null, “store_id”: 1, “order_items”: [ { “order_item_id”: 2, “uuid”: “6d882c0b-fd96-4b8d-ad4c-6237ce6d2e2d”, “order_id”: 2, “purchased_entity”: { “variation_id”: 6, “uuid”: “bea5ef34-3be5-4f8c-9473-500166cd90eb”, “type”: “clothing”, “product_id”: 1, “sku”: “DCHOODIE-GRE-LG”, “title”: “Drupal Commerce Hoodie – Green, Large”, “list_price”: { “number”: null, “currency_code”: null, “formatted”: null }, “price”: { “number”: “52.000000”, “currency_code”: “USD”, “formatted”: “$52.00” }, “attribute_color”: 2, “attribute_size”: 7, “field_images”: [ 4, 5, 6 ], “weight”: { “number”: “1.000000”, “unit”: “kg” } }, “title”: “Drupal Commerce Hoodie – Green, Large”, “quantity”: “6.00”, “unit_price”: { “number”: “52.000000”, “currency_code”: “USD”, “formatted”: “$52.00” }, “total_price”: { “number”: “312.00”, “currency_code”: “USD”, “formatted”: “$312.00” } }, { “order_item_id”: 3, “uuid”: “60338509-574d-40b1-8ac8-c92912129d4b”, “order_id”: 2, “purchased_entity”: { “variation_id”: 10, “uuid”: “d0bcaa47-dd2e-4938-978b-e9852ed2c846”, “type”: “simple”, “product_id”: 5, “sku”: “Favorit-VintageTypewriter”, “title”: “Vintage Typewriter”, “list_price”: { “number”: null, “currency_code”: null, “formatted”: null }, “price”: { “number”: “149.990000”, “currency_code”: “USD”, “formatted”: “$149.99” }, “field_images”: [ 10 ], “weight”: { “number”: “1.000000”, “unit”: “kg” } }, “title”: “Vintage Typewriter”, “quantity”: “6.00”, “unit_price”: { “number”: “149.990000”, “currency_code”: “USD”, “formatted”: “$149.99” }, “total_price”: { “number”: “899.94”, “currency_code”: “USD”, “formatted”: “$899.94” } } ], “total_price”: { “number”: “1211.94”, “currency_code”: “USD”, “formatted”: “$1,211.94” }, “coupons”: [], }
Please note, request method is PATCH here.
Remove order item from Cart
URL- http://yourproject.com/cart/2/items/3?_format=json
Request method: DELETE
Here 2 is the order_id which identifies our cart and 3 is the order item ID which we need to remove. You can see in response of GetCart, item has been removed.