Vue Storefront is now Alokai! Learn More
AddCartLineItem

AddCartLineItem

Implements AddCartLineItem Unified Method.

Source

import { defineApi, getCartVersion } from "@vsf-enterprise/unified-api-commercetools";
import { getProduct } from "@vsf-enterprise/unified-api-commercetools";
import type { CommercetoolsIntegrationContext } from "@vsf-enterprise/commercetools-api";
import type { AddCartLineItemArgs } from "@vsf-enterprise/unified-api-commercetools/udl";
import { getNormalizers } from "@vue-storefront/unified-data-model";

export const addCartLineItem = defineApi.addCartLineItem(async (context, args) => {
  if (args?.cartId != null) {
    throw { statusCode: 400, message: "Multiple carts feature is not available." };
  }
  const [variant, cartVersion] = await Promise.all([
    getProductVariant(context, args),
    getCartVersion(context),
  ]);

  if (!variant) {
    throw new Error("Variant not found");
  }

  const updatedCart = await context.api.addToCart(cartVersion, {
    product: variant,
    quantity: args.quantity || 1,
  });

  const { normalizeCart } = getNormalizers(context);

  return normalizeCart(updatedCart.cart);
});

async function getProductVariant(
  context: CommercetoolsIntegrationContext,
  args: AddCartLineItemArgs,
) {
  const product = await getProduct(context, { id: args.productId, sku: args.sku || undefined });
  const { allVariants, masterVariant } = product.masterData.current || {};

  if (!args.sku) {
    return masterVariant;
  }

  return allVariants?.find((variant) => variant.sku === args.sku);
}