Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
67 lines (51 sloc) 2.15 KB
id title
storefront-cart-modification
Implement cart modification

This article is part of the Storefront UI Development Guide.

Now that a shopper can view their cart, they'll likely want to change it. They already have the ability to add additional items to it, but at some point they'll want to change the quantity for an item or remove it entirely. We'll implement those actions in this task. There are additional cart mutations that will happen during a checkout flow, which we'll implement later.

Change the quantity for a cart item

The quantity change mutation is usually invoked by clicking a quantity increment or decrement button or entering or clicking a specific quantity. Regardless of what you do for the UI, all you need to do is figure out what new quantity the shopper wants, and then pass it to the updateCartItemsQuantity mutation.

mutation updateCartItemsQuantityMutation($input: UpdateCartItemsQuantityInput!) {
  updateCartItemsQuantity(input: $input) {
    cart {
      ...CartFragment
    }
  }
}

Where the input variable looks like this:

{
  cartId, // from application state
  items: [
    { cartItemId, quantity }
  ],
  token // from application state
}

cartItemId is the item._id and quantity is the new desired quantity, which must be an integer of 0 or greater. A quantity of 0 removes the item, but we recommend calling the removeCartItems mutation instead.

Remove a cart item

Removing a cart item is usually done by clicking a "Remove" button on the cart item UI. This should invoke the removeCartItems mutation.

mutation removeCartItemsMutation($input: RemoveCartItemsInput!) {
  removeCartItems(input: $input) {
    cart {
      ...CartFragment
    }
  }
}

Where the input variable looks like this:

{
  cartId, // from application state
  cartItemIds: [],
  token // from application state
}

cartItemIds is an array of IDs from item._id.

Next Task: Build a checkout page

You can’t perform that action at this time.