Vue Storefront is now Alokai! Learn More
ApplyCouponToCart

ApplyCouponToCart

Implements ApplyCouponToCart Unified Method.

Source

import { defineApi, getNormalizedCart } from "@vsf-enterprise/unified-api-sfcc";
import from "@vsf-enterprise/unified-api-sfcc/udl";

declare module "@vsf-enterprise/unified-api-sfcc" {
  interface ApplyCouponToCartExtendedArgs {}
}


export const applyCouponToCart = defineApi.applyCouponToCart(async (context, args) => {
  const { couponCode, cartId } = args;

  if (!couponCode) {
    throw new Error("The provided coupon code is empty and cannot be applied.");
  }

  const cart = await context.api.addCouponToBasket({ code: couponCode, basketId: cartId });

  return getNormalizedCart(context, cart);
});