Alokai
Basics

Cart

Introduction

This section will cover the basics of the cart and how to use it. We will also cover how to add, remove and clear products from the cart.

API Reference You can find all available CT module methods and their description in the API Reference.

Creating a cart

To create a cart, you can use the createCart method of the CT module.

const { cart } = await sdk.ct.createCart();

Store-scoped carts

If your commercetools project uses Stores - for example one store per market - you don't have to swap these calls for their store-scoped equivalents. The middleware resolves a store key from the vsf-store cookie and passes it to createCart, updateCart, and the checkout mutation, so the whole cart lifecycle is scoped for you.

Working with multiple stores

See Stores for how the store key is resolved, which methods use it, and how to override it per request.

Getting the cart

To get the cart, you can use the getMe method of the CT module.

const { me } = await sdk.ct.getMe();

const cart = me.cart;

Adding products

To add products to the cart, you can use the addToCart method of the CT module.

const { cart } = await sdk.ct.addToCart({
  cartId: '6931b5d2-986f-4d2b-8cba-45007a26eb5e',
  cartVersion: 1,
  sku: 'M0E20000000E59M',
  quantity: 1
});

Updating the cart

To update the cart, you can use the updateCart method of the CT module.

Updating cart with a single action.

const { cart } = await sdk.ct.updateCart({
  cartId: '6931b5d2-986f-4d2b-8cba-45007a26eb5e',
  cartVersion: 1,
  actions: [{ addDiscountCode: { code: 'code' } }]
});

Updating cart with multiple actions.

const { cart } = await sdk.ct.updateCart({
  cartId: '6931b5d2-986f-4d2b-8cba-45007a26eb5e',
  cartVersion: 1,
  actions: [
    { addDiscountCode: { code: 'code' } },
    { addLineItem: { quantity: 1, sku: 'M0E20000000DHRL' } }
  ]
});

Updating quantity of a product in the cart

To update the quantity of a product in the cart, you can use the updateCartQuantity method of the CT module.

const { cart } = await sdk.ct.updateCartQuantity({
  cartId: '6931b5d2-986f-4d2b-8cba-45007a26eb5e',
  cartVersion: 1,
  productId: 'ad7b9df8-8116-4c0e-a6fb-3ce37c98a6b3',
  quantity: 3
});

Removing products

To remove products from the cart, you can use the removeFromCart method of the CT module.

const { cart } = await sdk.ct.removeFromCart({
  cartId: 'cart-id',
  cartVersion: 1,
  product,
  quantity: 1
});

Clearing the cart

To clear the cart, you can use the deleteCart method of the CT module.

const { cart } = await sdk.ct.deleteCart({ id: '00035084', version: 1 });

Applying a coupon

To apply a coupon to the cart, you can use the applyCartCoupon method of the CT module.

const { cart } = await sdk.ct.applyCartCoupon({
  cartId: '6931b5d2-986f-4d2b-8cba-45007a26eb5e',
  cartVersion: 1,
  code: 'code'
});

Removing a coupon

To remove a coupon from the cart, you can use the removeCartCoupon method of the CT module.

const { cart } = await sdk.ct.removeCartCoupon({
  cartId: '6931b5d2-986f-4d2b-8cba-45007a26eb5e',
  cartVersion: 1,
  couponId: 'c2ae6cbb-5978-400e-b08e-6084b7c1bdb3'
});

On this page