Magento API add items to cart

Magento 2 API How to add items to cart

In this post, I will show you how to add items to the cart as a customer using Magento 2 API. Follow my examples of simple products, digital downloadable product, and customize product will give you an overview of how it can be done.

Step 1: Generate customer access token

We will add items to the cart on behalf of the customer, so Admin token is not required. Create a new request with this endpoint

POST <host>/rest/V1/integration/customer/token

Payload(body):

{ "username": "string", "password": "string" }
Code language: JSON / JSON with Comments (json)

Response: access token

Step 2: Add items to the cart

Create a new post with this endpoint:

POST <host>/rest/V1/carts/mine/items

Authorization: Bearer token

Enter your customer access token

Add a simple product

A simple product requires a SKU, the quantity, and the quote ID, follow our post to create a quote ID. The following example adds a Push It Messenger Bag (Magento sample product) (SKU: 24-WB04) to the cart.

Payload

{ "cartItem": { "sku": "24-WB04", "qty": 1, "quote_id": "42" } }
Code language: JSON / JSON with Comments (json)

Response

{ "item_id": 139, "sku": "24-WB04", "qty": 1, "name": "Push It Messenger Bag", "price": 45, "product_type": "simple", "quote_id": "42" }
Code language: JSON / JSON with Comments (json)
add simple item using api
Add a digital product

Downloadable items have the same requirements as simple product. The following example adds the downloadable product Solo Power Circuit (SKU: 240-LV07)

Payload

{ "cartItem": { "sku": "240-LV07", "qty": 1, "quote_id": "42" } }
Code language: JSON / JSON with Comments (json)

Response

{ "item_id": 140, "sku": "240-LV07", "qty": 1, "name": "Solo Power Circuit", "price": 14, "product_type": "downloadable", "quote_id": "42", "product_option": { "extension_attributes": { "downloadable_option": { "downloadable_links": [ 4 ] } } } }
Code language: JSON / JSON with Comments (json)
respon item
Add a customize product

What makes the configurable products different from other products is that you have to specify the SKU as well as the set of option_id/option_value pairs.

First, use this endpoint to get information about each combination of color and size. The following sample shows the returned values for size and color for a XS blue Proteus Fitness Jackshirt.

Get the option_id values for the given SKU.

GET <host>/rest/V1/configurable-products/:sku/options/all

Authorization: Bearer token – Admin token

get id value for the given sku

The following sample shows the returned values for size and color for a small blue Proteus Fitness Jackshirt (SKU: MJ12). The option_id values for Size and Color are 145 and 93.

GET <host>/rest/V1/configurable-products/:sku/children

Authorization: Bearer token – Customer token

Response

{ "custom_attributes": [ { "attribute_code": "size", "value": "166" }, { "attribute_code": "color", "value": "49" } ] }
Code language: JSON / JSON with Comments (json)

We now know the values for option_value for size and color are 166 and 49, so we’re ready to add the product to the cart.

POST <host>/rest/V1/carts/mine/items

Payload

{ "cartItem": { "sku": "MJ12", "qty": 1, "quote_id": "42", "product_option": { "extension_attributes": { "configurable_item_options": [ { "option_id": "93", "option_value": 49 }, { "option_id": "145", "option_value": 166 } ] } }, "extension_attributes": {} } }
Code language: JSON / JSON with Comments (json)
post api key using postman

Response

{ "item_id": 141, "sku": "MJ12-XS-Blue", "qty": 1, "name": "Proteus Fitness Jackshirt", "price": 45, "product_type": "configurable", "quote_id": "42", "product_option": { "extension_attributes": { "configurable_item_options": [ { "option_id": "93", "option_value": 50 }, { "option_id": "145", "option_value": 166 } ] } } }
Code language: JSON / JSON with Comments (json)
respone product in postman
Add a bundle product

You have to add individual items to the bundle product by specifying the id defined in the item’s product_links object. The product_links links an item’s SKU and id to the SKU of the bundle product.

In this example, we will use the Sprite Yoga Companion Kit (SKU: 24-WG080), which includes:

  • Statis Ball (55cm, 65cm, 75cm)
  • Foam Yoga brick
  • Yoga Strap (6ft, 8ft, 10ft)
  • Foam Roller

First, we get the bundle product id

GET <host>/rest/V1/bundle-products/24-WG080/options/all

Authorization: Bearer token – Admin token

Response

[ { "option_id": 1, "title": "Sprite Stasis Ball", "required": true, "type": "radio", "position": 1, "sku": "24-WG080", "product_links": [ { "id": "1", "sku": "24-WG081-blue", "option_id": 1, "qty": 1 }, { "id": "2", "sku": "24-WG082-blue", "option_id": 1, "qty": 1 }, { "id": "3", "sku": "24-WG083-blue", "option_id": 1, "qty": 1 } ] }, { "option_id": 2, "title": "Sprite Foam Yoga Brick", "required": true, "type": "radio", "position": 2, "sku": "24-WG080", "product_links": [ { "id": "4", "sku": "24-WG084", "option_id": 2, "qty": 1 } ] }, { "option_id": 3, "title": "Sprite Yoga Strap", "required": true, "type": "radio", "position": 3, "sku": "24-WG080", "product_links": [ { "id": "5", "sku": "24-WG085", "option_id": 3, "qty": 1 }, { "id": "6", "sku": "24-WG086", "option_id": 3, "qty": 1 }, { "id": "7", "sku": "24-WG087", "option_id": 3, "qty": 1 } ] }, { "option_id": 4, "title": "Sprite Foam Roller", "required": true, "type": "radio", "position": 4, "sku": "24-WG080", "product_links": [ { "id": "8", "sku": "24-WG088", "option_id": 4, "qty": 1 } ] } ]
Code language: JSON / JSON with Comments (json)

Then specify the product options that you want to choose. For example:

  • 55 cm Sprite Stasis Ball (id: 1)
  • Sprite Foam Yoga Brick (id: 4)
  • 6 ft Sprite Yoga strap (id: 5)
  • Sprite Foam Roller (id: 8)

Create a new request with this endpoint:

POST /rest/V1/carts/mine/items

Authorization: Bearer token – Customer token

Payload

{ "cartItem": { "sku": "24-WG080", "qty": 1, "quote_id": "4", "product_option": { "extension_attributes": { "bundle_options": [ { "option_id": 1, "option_qty": 1, "option_selections": [1] }, { "option_id": 2, "option_qty": 1, "option_selections": [4] }, { "option_id": 3, "option_qty": 1, "option_selections": [5] }, { "option_id": 4, "option_qty": 1, "option_selections": [8] } ] } } } }
Code language: JSON / JSON with Comments (json)

Response

{ "item_id": 9, "sku": "24-WG080-24-WG084-24-WG088-24-WG082-blue-24-WG086", "qty": 1, "name": "Sprite Yoga Companion Kit", "price": 68, "product_type": "bundle", "quote_id": "4", "product_option": { "extension_attributes": { "bundle_options": [ { "option_id": 1, "option_qty": 1, "option_selections": [ 1 ] }, { "option_id": 2, "option_qty": 1, "option_selections": [ 4 ] }, { "option_id": 3, "option_qty": 1, "option_selections": [ 5 ] }, { "option_id": 4, "option_qty": 1, "option_selections": [ 8 ] } ] } } }
Code language: JSON / JSON with Comments (json)

Step 3: Verify the result

Log in with your customer account and check your cart for all the items you have added.

Above I have shown you how to add every type of item to a shopping cart using Magento 2 API. Hope you successfully generate products. For more guidess, visit our blog or Magento Devdocs.

Related Articles

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on print
Share on email