1 ---
2 tags:
3 - cart
4 - storefront
5 products:
6 - cart
7 ---
8
9 import { CodeTabs, CodeTab } from "docs-ui"
10
11 export const metadata = {
12 title: `Manage Cart's Items in Storefront`,
13 }
14
15 # {metadata.title}
16
17 In this guide, you'll learn how to manage a cart's line items, including adding, updating, and removing them.
18
19 ## Add Product Variant to Cart
20
21 To add a product variant to a cart, use the [Add Line Item API route](!api!/store/carts/add-line-item).
22
23 <Note title="Tip">
24
25 To retrieve a variant's available quantity and check if it's in stock, refer to the [Retrieve Product Variant's Inventory](../../products/inventory/page.mdx) guide.
26
27 </Note>
28
29 For example:
30
31 <Note title="Tip">
32
33 Learn how to install and configure the JS SDK in the [JS SDK documentation](../../../js-sdk/page.mdx).
34
35 </Note>
36
37 export const addHighlights = [
38 ["1", "variant_id", "The ID of the selected variant."],
39 ["2", "cartId", "Retrieve the cart ID from the `localStorage`."],
40 ["10", "quantity", "You can also allow customers to specify the quantity."]
41 ]
42
43 ```ts highlights={addHighlights}
44 const addToCart = (variant_id: string) => {
45 const cartId = localStorage.getItem("cart_id")
46
47 if (!cartId) {
48 return
49 }
50
51 sdk.store.cart.createLineItem(cartId, {
52 variant_id,
53 quantity: 1,
54 })
55 .then(({ cart }) => {
56 // use cart
57 console.log(cart)
58 alert("Product added to cart")
59 })
60 }
61 ```
62
63 The [Add Line Item API route](!api!/store/carts/add-line-item) requires two request body parameters:
64
65 - `variant_id`: The ID of the product variant to add to the cart. This is the variant selected by the customer.
66 - `quantity`: The quantity to add to cart.
67
68 The API route returns the updated [cart object](!api!/store/carts/schema).
69
70 ---
71
72 ## Update Line Item in Cart
73
74 You can update the quantity of a line item in the cart using the [Update Line Item API route](!api!/store/carts/update-line-item).
75
76 For example:
77
78 export const updateHighlights = [
79 ["2", "itemId", "The ID of the item to update."],
80 ["3", "quantity", "The new quantity of the item."],
81 ["5", "cartId", "Retrieve the cart ID from the `localStorage`."],
82 ["11", "itemId", "Pass the item's ID as a parameter."],
83 ]
84
85 ```ts highlights={updateHighlights}
86 const updateQuantity = (
87 itemId: string,
88 quantity: number
89 ) => {
90 const cartId = localStorage.getItem("cart_id")
91
92 if (!cartId) {
93 return
94 }
95
96 sdk.store.cart.updateLineItem(cartId, itemId, {
97 quantity,
98 })
99 .then(({ cart }) => {
100 // use cart
101 console.log(cart)
102 })
103 }
104 ```
105
106 The [Update Line Item API route](!api!/store/carts/update-line-item) requires:
107
108 - The line item's ID to be passed as a path parameter.
109 - The `quantity` request body parameter, which is the new quantity of the item.
110
111 The API route returns the updated [cart object](!api!/store/carts/schema).
112
113 ---
114
115 ## Remove Line Item from Cart
116
117 To remove a line item from the cart, send a request to the [Remove Line Item API route](!api!/store/carts/remove-line-item).
118
119 For example:
120
121 export const deleteHighlights = [
122 ["1", "itemId", "The ID of the line item to remove."],
123 ["2", "cartId", "Retrieve the cart ID from the `localStorage`."],
124 ["8", "itemId", "Pass the item's ID as a parameter."],
125 ["9", "parent", "The updated cart is returned as the `parent` field."]
126 ]
127
128 ```ts highlights={deleteHighlights}
129 const removeItem = (itemId: string) => {
130 const cartId = localStorage.getItem("cart_id")
131
132 if (!cartId) {
133 return
134 }
135
136 sdk.store.cart.deleteLineItem(cartId, itemId)
137 .then(({ parent: cart }) => {
138 // use cart
139 console.log(cart)
140 })
141 }
142 ```
143
144 The [Delete Line Item API route](!api!/store/carts/remove-line-item) returns the updated [cart object](!api!/store/carts/schema) as the `parent` field.