Skip to main content
More in Learn

Update Cart

Now that we have covered how to get the auth token and hydrate the cart with the Shop API, there are actions a user will perform such as adding item to a cart or removing an existing item from the cart. Let’s take a look at the different mutations available to take care of such actions.

addSkuItem

Add an item that already exists in Crystallize. The mutation takes the following variables as input arguments:

  • id: Cart ID
  • input
    • sku: SKU of the item in Crystallize
    • quantity: amount to increase the item count by (defaults to 1 if not provided)
    • Additionally, it can also take in meta and tax information.
mutation addSkuItem {
    addSkuItem(id: "CART_ID", input: {sku: "hammer-mjolnir"}) {
	    id
    }
}

addExternalItem

This mutation manages items that do not exist in Crystallize, but reside somewhere else instead. It takes in the following input arguments:

  • id: Cart ID
  • input
    • sku: SKU of the external item
    • quantity: amount to increase the item count by (defaults to 1 if not provided)
    • name: name of the external item
    • variant: information about the variant such as price (net, gross, discounts), product information such as its ID and path
  • Additionally, it can also take in images and meta information.
mutation addExternalItem {
	addExternalItem(id: "CART_ID", input: {
		sku: "hammer-mjolnir"
		name: "Mjolnir"
		variant: {
			price: {
				net: 500.00
				gross: 550.00
			}
			product: {
				id: "4389955"
				path: "/products/hammer/mjolnir"
			}
		}
	}) {
		id
	}
}

setCartItem

This is a generic way of adding an item to cart, regardless of whether it exists in Crystallize or not. It will update the quantity of an item, but will flag it as unmanaged. What that means is, unlike addSkuItem which will fetch data from Crystallize based on the SKU, setCartItem will not. This mutation takes in the same input arguments as setExternalItem:

  • id: Cart ID
  • input
    • sku: SKU of the external item
    • quantity: amount to increase the item count by (defaults to 1 if not provided)
    • name: name of the external item
    • variant: information about the variant such as price (net, gross, discounts), product information such as its ID and path
  • Additionally, it can also take in images and meta information.
mutation setCartItem {
	setCartItem(id: "CART_ID", input: {
		sku: "hammer-mjolnir"
		name: "Mjolnir"
		variant: {
			price: {
				net: 500.00
				gross: 550.00
			}
			product: {
				id: "4389955"
				path: "/products/hammer/mjolnir"
			}
		}
	}) {
		id
	}
}

removeCartItem

As the name suggests, this mutation removes an item from the cart. Required arguments include:

  • id: Cart ID
  • sku: SKU of the item
  • quantity: amount to decrease the item count by (defaults to 1 if not provided)
mutation removeCartItem {
	removeCartItem(id: "CART_ID", sku: "mjolnir-hammer", quantity: 2){
		id	
	}
}

addCartItem

This mutation simply increases the quantity of an item that is already in the cart. If you're trying to add an item that's not already in the cart, use addSkuItem or addExternalItem instead.

This mutation takes in the following input arguments:

  • id: Cart ID
  • sku: SKU of the item
  • quantity: amount to increase the item count by (defaults to 1 if not provided)
mutation addCartItem {
	addCartItem(id: "CART_ID", sku: "mjolnir-hammer", quantity: 2){
		id
	}
}

changeCartItemPricing

Mutation to run when you would like to change the price and the quantity of an item. Please note that this mutation also marks the item as unflagged, meaning the item information will then not be fetched from Crystallize. Input arguments include:

  • sku: SKU of the item
  • quantity: amount to increase the item count by (defaults to 1 if not provided)
  • price: net, gross, and furthermore discounts if applicable
mutation changeCartItemPricing {
	changeCartItemPricing(id: "CART_ID", input: {
		sku: "mjolnir-hammer"
		quantity: 2
		price: {
			net: 500.00
			gross: 550.00
		}
	}){
		id
	}
}

setCustomer

You most likely have a customer associated with each cart. You can set the customer for a particular cart using this mutation, which requires the following:

  • id: Cart ID
  • isGuest: a boolean value (true or false)
  • identifier: unique customer identifier (email in this example)
  • Other personal information such as first name, last name, middle name, phone number, email, birth date, company name, etc.
  • addresses: can take in one address as an object or multiple types of addresses as an array
  • Additionally, you can also have meta information and external references to a customer.
mutation setCustomer {
	setCustomer(id: "CART_ID", input: {
		isGuest: false 
		identifier: "legolas@gmail.com" 
		firstName: "Legolas" 
		lastName: "Greenleaf" 
		phone: "12345678" 
		email: "legolas@gmail.com"
		addresses: { 
			type: delivery 
			streetNumber: "16" 
			street: "Greenwood" 
			city: "Woodland Realm" 
			country: "Ithilien" 
			postalCode: "1408" 
		}
	}){
		id
	}
}

setMeta

Lastly, if you would like to set meta information on the cart as whole, the setMeta mutation comes into play. Input arguments include:

  • id: Cart ID
  • meta: an array of key value pairs
mutation setMeta {
	setMeta(id: "CART_ID", meta: [{
		key: "Paid via"
		value: "invoice"
	}]) {
		id
	}
}
People showing thumbs up

Need further assistance?

Ask the Crystallize team or other enthusiasts in our slack community.

Join our slack community