Order Processing with REST APIs Tutorial
Step 5. Add items to the cart
This article shows how to add a simple product, a downloadable product, and a bundle product to the cart.
All calls are performed on behalf of a customer, and the customer’s token is specified in the authorization header.
Add a simple product to a cart
To add a simple product to a cart, you must provide a sku
, the quantity, and the quote ID, which was generated when the cart was created.
The following example adds an orange medium-sized Radiant women’s t-shirt (sku
: WS12-M-Orange
) to the cart.
Endpoint
POST http://<host>/rest/default/V1/carts/mine/items
Headers
Content-Type
application/json
Authorization
Bearer <customer token>
Payload
Response
Add a downloadable product to a cart
The requirements for adding a downloadable product to a cart are the same as a simple product. You must specify the sku
, the quantity, and quote ID.
The following example adds the downloadable product Advanced Pilates & Yoga (sku
: 240-LV08)
Endpoint
POST http://<host>/rest/default/V1/carts/mine/items
Headers
Content-Type
application/json
Authorization
Bearer <customer token>
Payload
Response
Add a configurable product to a cart
To add a configurable product to a cart, you must specify the sku
as well as the set of option_id
/option_value
pairs that make the product configurable.
In this example, we’ll add the Chaz Kangeroo Hoodie (sku: MH01
) configurable product to the cart. This product comes in three colors (black, gray, and orange) and five sizes (XS, S, M, L, XL). In the sample data, the option_id
values for Size and Color are 141
and 93
, respectively. You can use the GET /V1/configurable-products/:sku/options/all
call to determine the option_id
values for the given SKU.
The GET /V1/configurable-products/:sku/children
call returns information about each combination of color and size, 15 in all for MH01
. The following sample shows the returned values for size
and color
for a small gray Chaz Kangeroo Hoodie.
We now know the values for option_value
for size
and color
are 168
and 52
, so we’re ready to add the product to the cart.
Endpoint
POST http://<host>/rest/default/V1/carts/mine/items
Headers
Content-Type
application/json
Authorization
Bearer <customer token>
Payload
Response
Add a bundle product to a cart
The sample data provides one bundled product, the Sprite Yoga Companion Kit (sku
: 24-WG080
). The kit contains the following items:
- Sprite Statis Ball in sizes 55 cm (
sku
:24-WG081-blue
), 65 cm (sku
:24-WG082-blue
), or 75 cm (sku
:24-WG083-blue
) - Sprite Foam Yoga brick (
sku
:24-WG084
) - Sprite Yoga Strap in lengths 6 ft (
sku
:24-WG085
), 8 ft (sku
:24-WG086
), or 10 ft (sku
:24-WG087
) - Sprite Foam Roller (
sku
:24-WG088
)
To add a bundle product to a cart, you must specify the sku
of the bundle product, but not the individual items. You add individual items to the bundle product by specifying the id
defined in the item’s product_links
object. The product_links
object primarily describes the ordering and placement of options on the customization page, but it also links an item’s sku
and id
to the sku
of the bundle product.
The GET http://<host>/rest/default/V1/bundle-products/24-WG080/options/all
call returns id
values, as shown in the following simplified response:
For this example, we’ll configure the Sprite Yoga Companion Kit as follows:
- 65 cm Sprite Stasis Ball (
id
:2
) - Sprite Foam Yoga Brick (
id
:4
) - 8 ft Sprite Yoga strap (
id
:6
) - Sprite Foam Roller (
id
:8
)
Endpoint
POST http://<host>/rest/default/V1/carts/mine/items
Headers
Content-Type
application/json
Authorization
Bearer <customer token>
Payload
Response
Verify this step
Sign in as the customer and click on the shopping cart. These three items are displayed.