Skip to main content
Version: 2.0.0

AddProductsToCartMutation

Description

Adds one or more products to a cart. Pass the cartId and an array of AddCartItemInput objects — each specifying a SKU, an optional quantity, and optional custom data (matrix variant selection, gift card value, etc.).

If a product matching the given SKU is already in the cart, its quantity is incremented rather than a duplicate line being created.

tip

You can add multiple products in a single call by providing more than one entry in the items array. The mutation returns the full updated cart, so there is no need to follow up with a cart query.

Endpoint

mutation addProductsToCart($cartId: String!, $items: [AddCartItemInput!]) {
addProductsToCart(cartId: $cartId, items: $items) {
# Return fields
}
}

Arguments

ArgumentTypeRequiredDescription
cartIdStringYesThe unique identifier of the cart to which products will be added. This ID is obtained from createGuestCart or createCustomerCart mutations.
siteIdIntNoThe site ID where the cart is created. Defaults to 1 if not provided.
items[AddCartItemInput]NoA list of items to add to the cart.

AddCartItemInput

FieldTypeRequiredDescription
skuStringYesThe SKU or handle of the product to add to the cart.
qtyIntNoThe quantity of the product to add. Defaults to 1 if not provided.
customObjectNoCustom data for the cart item.

Custom Data Fields

The custom object can contain the following special fields:

FieldTypeDescription
_attributeYStringY-axis attribute for product matrix selection.
_attributeXStringX-axis attribute for product matrix selection.
_giftCardValueFloatCustom value for gift cards.

Additional custom fields can be included as needed and will be stored with the cart item.

Return Values

The mutation returns a CartType object with the following structure:

FieldTypeDescription
billing_addressObjectThe billing address associated with the cart.
shipping_addressObjectThe shipping address associated with the cart.
items[CartItem]Array of items in the cart.
selected_shipping_methodObjectThe selected shipping method for the cart.
selected_payment_methodObjectThe selected payment method for the cart.
totalsObjectCart totals information.

CartItem Structure

Each item in the items array contains:

FieldTypeDescription
idStringUnique identifier for the cart item.
quantityIntQuantity of the product in the cart.
tax_percentFloatTax percentage applied to this item.
total_weightFloatTotal weight of this item.
nameStringProduct name.
skuStringProduct SKU.
thumbnailStringURL to the product thumbnail image.
customObjectCustom data associated with this item.
priceMoneyBase price of the product.
price_tax_amountMoneyTax amount for a single unit.
price_including_taxMoneyPrice including tax for a single unit.
price_excluding_taxMoneyPrice excluding tax for a single unit.
row_totalMoneyTotal price for this item (quantity × price).
row_total_tax_amountMoneyTotal tax amount for this item.
row_total_including_taxMoneyTotal price including tax for this item.
row_total_excluding_taxMoneyTotal price excluding tax for this item.

Money Structure

Price fields use the Money structure:

FieldTypeDescription
valueFloatThe numerical value.
currencyStringThe currency code (e.g., "USD").

Usage Example

mutation {
addProductsToCart(
cartId: "abc123",
items: [
{
sku: "product-123",
qty: 2,
custom: {
_attributeY: "color",
_attributeX: "red"
}
}
]
) {
items {
id
quantity
name
sku
price {
value
currency
}
row_total {
value
currency
}
}
totals {
grand_total {
value
currency
}
}
}
}

Example Response

{
"data": {
"addProductsToCart": {
"items": [
{
"id": "123",
"quantity": 2,
"name": "Sample Product",
"sku": "product-123",
"price": {
"value": 19.99,
"currency": "USD"
},
"row_total": {
"value": 39.98,
"currency": "USD"
}
}
],
"totals": {
"grand_total": {
"value": 39.98,
"currency": "USD"
}
}
}
}
}

Notes

  • Products are identified by their SKU/handle, not by their numeric ID.
  • If the product is already in the cart the specified quantity is added to the existing line — a duplicate item is not created.
  • Stock validation runs when catalog stock management is enabled. An error is returned if the requested quantity exceeds available stock.
  • If guest checkout is disabled and no authenticated customer is present, the mutation returns an error.
  • _attributeY / _attributeX in custom are for matrix-variation products. Pass the axis labels (e.g. "color" / "red"), not the display names.
  • _giftCardValue is only valid for gift card product types and is ignored otherwise.
warning

Always obtain a valid cartId via createGuestCart or createCustomerCart before calling this mutation. Passing an unknown or expired cart ID returns an error.