UpdateCartItemsMutation
Description
Updates the quantities of one or more items in the cart. This mutation allows you to change the quantity of existing products in the cart based on their SKUs.
Endpoint
mutation updateCartItems($cartId: String!, $items: [UpdateCartItemInput!]) {
updateCartItems(cartId: $cartId, items: $items) {
# Return fields
}
}
Arguments
| Argument | Type | Required | Description |
|---|---|---|---|
| cartId | String | Yes | The unique identifier of the cart to update. This ID is obtained from createGuestCart or createCustomerCart mutations. |
| items | [UpdateCartItemInput] | No | A list of items to update in the cart. |
UpdateCartItemInput
| Field | Type | Required | Description |
|---|---|---|---|
| sku | String | Yes | The SKU of the product to update. |
| qty | Int | Yes | The new quantity for the product. Set to 0 to remove the item from the cart. |
Return Values
The mutation returns a CartType object with the following structure:
| Field | Type | Description |
|---|---|---|
| billing_address | Object | The billing address associated with the cart. |
| shipping_address | Object | The shipping address associated with the cart. |
| items | [CartItem] | Array of items in the cart, with updated quantities. |
| selected_shipping_method | Object | The selected shipping method for the cart. |
| selected_payment_method | Object | The selected payment method for the cart. |
| totals | Object | Cart totals information, recalculated after updating the items. |
For detailed information about the structure of these fields, refer to the CartQuery documentation.
Usage Example
mutation {
updateCartItems(
cartId: "abc123",
items: [
{
sku: "product-123",
qty: 3
},
{
sku: "product-456",
qty: 1
}
]
) {
items {
id
quantity
name
sku
row_total {
value
currency
}
}
totals {
grand_total {
value
currency
}
}
}
}
Example Response
{
"data": {
"updateCartItems": {
"items": [
{
"id": "123",
"quantity": 3,
"name": "Sample Product",
"sku": "product-123",
"row_total": {
"value": 59.97,
"currency": "USD"
}
},
{
"id": "456",
"quantity": 1,
"name": "Another Product",
"sku": "product-456",
"row_total": {
"value": 29.99,
"currency": "USD"
}
}
],
"totals": {
"grand_total": {
"value": 89.96,
"currency": "USD"
}
}
}
}
}
Notes
- If you set the quantity of an item to 0, it will be removed from the cart.
- If the SKU does not exist in the cart, the update for that item will be ignored.
- After updating items, the cart totals will be recalculated.
- If you want to remove an item from the cart, you can either use this mutation with a quantity of 0, or use the
removeItemFromCartmutation. - Stock validation may be performed if catalog stock management is enabled. An error will be thrown if the requested quantity exceeds available stock.