Vue Storefront is now Alokai! Learn More
AddCartLineItem

AddCartLineItem

Implements AddCartLineItem Unified Method.

Source

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

declare module "@vsf-enterprise/unified-api-sfcc" {
  interface AddCartLineItemExtendedArgs {
    /**
     * The additional lineItem object fields to be updated in a cart.
     * {@link https://developer.salesforce.com/docs/commerce/commerce-api/references/shopper-baskets?meta=addItemToBasket SFCC method reference}
     */
    lineItem?: Omit<BasketProductAddParams, "productId" | "quantity">;
  }
}


export const addCartLineItem = defineApi.addCartLineItem(async (context, args) => {
  const { quantity = 1, sku, cartId } = args;

  if (!sku) {
    throw new Error("sku parameter is required");
  }

  const cart = await context.api.addProductsToBasket({
    basketId: cartId,
    items: [
      {
        productId: sku,
        quantity,
      },
    ],
  });

  return await getNormalizedCart(context, cart);
});