Vue Storefront is now Alokai! Learn More
ApplyCouponToCart

ApplyCouponToCart

Implements ApplyCouponToCart Unified Method.

Source

import { HttpStatusCode } from "@alokai/connect/middleware";

import { defineApi, getCartId } from "@vsf-enterprise/unified-api-bigcommerce";
import { methods } from "@vsf-enterprise/unified-api-bigcommerce";
const { getCart } = methods;

export const applyCouponToCart = defineApi.applyCouponToCart(async (context, args) => {
  if (args.cartId != null) {
    throw context.createHttpError({
      message: "Multiple carts feature is not available.",
      statusCode: HttpStatusCode.BAD_REQUEST,
    });
  }
  const cartId = getCartId(context);

  // response doesn't include LineItemsPhysicalItemsOptions, so we have to get cart again
  const { api } = await context.getApiClient();
  await api.addCoupon({
    checkoutId: cartId,
    couponCode: args.couponCode,
  });

  return getCart(context);
});