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.
Add an item that already exists in Crystallize. The mutation takes the following variables as input arguments:
mutation addSkuItem {
addSkuItem(id: "CART_ID", input: {sku: "hammer-mjolnir"}) {
id
}
}mutation addSkuItem {
addSkuItem(id: "CART_ID", input: {sku: "hammer-mjolnir"}) {
id
}
}This mutation manages items that do not exist in Crystallize, but reside somewhere else instead. It takes in the following input arguments:
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
}
}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
}
}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:
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
}
}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
}
}As the name suggests, this mutation removes an item from the cart. Required arguments include:
mutation removeCartItem {
removeCartItem(id: "CART_ID", sku: "mjolnir-hammer", quantity: 2){
id
}
}mutation removeCartItem {
removeCartItem(id: "CART_ID", sku: "mjolnir-hammer", quantity: 2){
id
}
}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:
mutation addCartItem {
addCartItem(id: "CART_ID", sku: "mjolnir-hammer", quantity: 2){
id
}
}mutation addCartItem {
addCartItem(id: "CART_ID", sku: "mjolnir-hammer", quantity: 2){
id
}
}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:
mutation changeCartItemPricing {
changeCartItemPricing(id: "CART_ID", input: {
sku: "mjolnir-hammer"
quantity: 2
price: {
net: 500.00
gross: 550.00
}
}){
id
}
}mutation changeCartItemPricing {
changeCartItemPricing(id: "CART_ID", input: {
sku: "mjolnir-hammer"
quantity: 2
price: {
net: 500.00
gross: 550.00
}
}){
id
}
}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:
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
}
}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
}
}Lastly, if you would like to set meta information on the cart as whole, the setMeta mutation comes into play. Input arguments include:
mutation setMeta {
setMeta(id: "CART_ID", meta: [{
key: "Paid via"
value: "invoice"
}]) {
id
}
}mutation setMeta {
setMeta(id: "CART_ID", meta: [{
key: "Paid via"
value: "invoice"
}]) {
id
}
}