Magento 2 API – Create Cart & Add Products to Cart
End-to-end Magento integration
Integrate any app or service. Consult our API development experts.
Hello, Magento peeps!
In my previous blog post, I talked about resetting the customer password using Magento 2 API. In case you missed it, you can read it at Magento 2 API – Reset Customer Password.
Today, I will show you we can create a cart and add products. In case you are developing a new storefront or mobile app based on Magento 2, you may require using a set of Magento 2 APIs to integrate everything together. To allow the customers to add products to the cart, the client must call an API request to Magento 2 to create a cart first. In this blog post, I will guide you completely through the Magento 2 API for creating a cart and adding products to it.
So, let’s begin with the Magento 2 API – Create Cart & Add Products to Cart!
How to Create a Cart and Add Products to It Using Magento 2 API?
The API request to create a cart and add products to it in Magento 2 requires customer-level access, and therefore the client must pass the customer token to validate the request. The customer token is generated by Magento 2 after the validation of the customer’s credentials. You may love to read this blog post on Magento 2 API – Get Customer Token.
In order to add products to the cart, the client first needs to send a cart creation request to Magento 2 API. The request returns a quote ID, which can be further used to add items to the cart using Magento 2 API.
Step 1: Create a Cart Using Magento 2 API (Magento 2 REST API Get Cart Id)
In order to get the cart id using Magento 2 API, the client needs to send a POST request to the store_url/rest/default/V1/carts/mine endpoint with the customer token in the header. An example of the same is provided below.
Method: POST
URL: store_url/rest/default.V1/carts/mine
Header:
- Bearer Token: <Customer_token>
Response:
Once the customer token is validated, the API returns the quote id, which can be later used for adding or removing products from the cart using Magento 2 API.
Step 2: Add Products to Cart Using Magento 2 API
We have successfully created a cart in Magento 2, and now it’s time to add items to it. Adding items to the cart using Magento 2 REST API requires the product SKU, quantity, and quote id of the cart. The client needs to send a POST request to the store_url/rest/default/V1/carts/mine/items endpoint along with the required details in the payload and customer access token to add products to the cart using Magento 2 rest API. Let’s understand it in a better way, using an example.
Method: POST
URL: store_url/rest/V1/carts/mine/items
Header:
- Bearer Token: <Customer_token>
Body:
1 2 3 4 5 |
{ "cartItem":{ "sku": "Tshirt", "qty": 1, "quote_id": "51"} } |
Response:
On successful authorization of the customer token, Magento 2 responds with the following information:
1 2 3 4 5 6 7 8 9 |
{ "item_id": 81, "sku": "Tshirt", "qty": 1, "name": "Tshirt", "price": 100, "product_type": "simple", "quote_id": "82" } |
That’s it!
You’ve successfully created a cart and added products to it using Magento 2 API.
Conclusion
A shopping cart is one of the must-have functionalities for online stores and shopping apps. The Magento 2 API to create a cart and products to it is helpful for creating such functionalities in third-party developments based on Magento 2. I hope this blog post will help you while creating such functionalities in your third-party development. In case you still have any queries or doubts regarding Magento 2 API, feel free to comment. I will be happy to help you.😃
Also, do not forget to share this blog post with your developer friends via social media.😊
Thanks for reading.🍀
◄ Magento 2 API – Reset Customer PasswordMagento 2 API – Set Shipping & Billing Information ►
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
Magento 2 API – Create Order
Best Resources to Learn Magento 2 From Scratch
Next