The Vue Storefront Essentials Course is now available! Learn More
SetShippingMethod

SetShippingMethod

Implements SetShippingMethod Unified Method.

Source

import { defineApi, getCartVersion, getNormalizerContext } from "@vsf-enterprise/unified-api-commercetools";
import { cartActions } from "@vsf-enterprise/commercetools-api";
import type { Cart } from "@vsf-enterprise/commercetools-types";
import { getNormalizers } from "@vue-storefront/unified-data-model";

export const setShippingMethod = defineApi.setShippingMethod(async (context, args) => {
  const version = await getCartVersion(context);

  const updatedCart = await context.api.updateCart({
    ...version,
    actions: [cartActions.setShippingMethodAction(args.shippingMethodId)],
  });

  const { normalizeCart } = getNormalizers(context);

  return normalizeCart(updatedCart.data?.cart as Cart, getNormalizerContext(context));
});