Vue Storefront is now Alokai! Learn More
Cart

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.

import { sdk } from '~/sdk.config.ts';

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

Getting the cart

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

import { sdk } from '~/sdk.config.ts';

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

const cart = me.cart;

Adding products

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

import { sdk } from '~/sdk.config.ts';

const { cart } = await sdk.ct.addProductToCart({
  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.

import { sdk } from '~/sdk.config.ts';

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

Updating cart with multiple actions.

import { sdk } from '~/sdk.config.ts';

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.

import { sdk } from '~/sdk.config.ts';

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 removeProductFromCart method of the CT module.

import { sdk } from '~/sdk.config.ts';

const { cart } = await sdk.ct.removeProductFromCart({
  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.

import { sdk } from '~/sdk.config.ts';

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.

import { sdk } from '~/sdk.config.ts';

const { cart } = await sdk.commerce.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.

import { sdk } from '~/sdk.config.ts';

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