Skip to main content
Version: 1.0.0

AddProductsToCartMutation

Description

Adds one or more products to the cart. This mutation allows you to specify product SKUs, quantities, and custom attributes for each item being added.

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 ID.
  • If the product is already in the cart, this mutation will add the specified quantity to the existing quantity.
  • Stock validation is performed if catalog stock management is enabled. An error will be thrown if the requested quantity exceeds available stock.
  • Guest checkout validation is performed. An error will be thrown if guest checkout is disabled and the user is not logged in.
  • The attribute matrix (attributeX and attributeY) is used for products with matrix-based variations.
  • Custom gift card values are only applicable for gift card products.