Skip to content

useCart

useCart

Category:
Cart & Checkout

Types

ts
export function useCartFunction(): UseCartReturn

source code

ts
export type UseCartReturn = {
  /**
   * Add product by id and quantity
   */
  addProduct(params: {
    id: string;
    quantity?: number;
  }): Promise<Schemas["Cart"]>;
  /**
   * Add products by array of items
   */
  addProducts(
    items: operations["addLineItem post /checkout/cart/line-item"]["body"]["items"],
  ): Promise<Schemas["Cart"]>;
  /**
   * Adds a promotion code to the cart
   */
  addPromotionCode(promotionCode: string): Promise<Schemas["Cart"]>;
  /**
   * Lists all applied and active promotion codes
   */
  appliedPromotionCodes: ComputedRef<Schemas["LineItem"][]>;
  /**
   * Current Cart object
   */
  cart: ComputedRef<Schemas["Cart"] | undefined>;
  /**
   * All items in the cart
   */
  cartItems: ComputedRef<Schemas["LineItem"][]>;
  /**
   * Changes the quantity of a product in the cart
   */
  changeProductQuantity(params: {
    id: string;
    quantity: number;
  }): Promise<Schemas["Cart"]>;
  /**
   * The number of items in the cart
   */
  count: ComputedRef<number>;
  /**
   * Refreshes the cart object and related data
   * If @param newCart is provided, it will be used as a new cart object
   */
  refreshCart(newCart?: Schemas["Cart"]): Promise<Schemas["Cart"]>;
  /**
   * Removes the provided LineItem from the cart
   */
  removeItem(lineItem: Schemas["LineItem"]): Promise<Schemas["Cart"]>;
  /**
   * The total price of the cart (including calculated costs like shipping)
   */
  totalPrice: ComputedRef<number>;
  /**
   * Shipping price
   */
  shippingTotal: ComputedRef<number>;
  /**
   * The total price of all cart items
   */
  subtotal: ComputedRef<number>;
  /**
   * `true` if the cart contains no items
   */
  isEmpty: ComputedRef<boolean>;
  /**
   * `true` if cart contains only digital items
   */
  isVirtualCart: ComputedRef<boolean>;
  /**
   * Get cart errors
   */
  consumeCartErrors(): Schemas["Cart"]["errors"];
};

source code