Vue Storefront is now Alokai! Learn More
SetCartAddress

SetCartAddress

Implements SetCartAddress Unified Method.

Source

import { defineApi, getCartId, query } from "@vsf-enterprise/unified-api-magento";
import type { Cart, CartAddressInput } from "@vsf-enterprise/unified-api-magento/ecommerceTypes";
import { getNormalizers } from "@vsf-enterprise/unified-api-magento/udl";

export const setCartAddress = defineApi.setCartAddress(async (context, args) => {
  const cartId = getCartId(context);
  const { unnormalizeAddress, unnormalizeCustomerAddress, normalizeCart } = getNormalizers(context);
  /**
   *  `CartAddressInput` does not support titleCode (prefix) field
   *  {@link https://docs.alokai.com/integrations/magento/api/magento-types/CartAddressInput}
   */
  args.shippingAddress.titleCode = null;
  const address =
    "id" in args.shippingAddress
      ? unnormalizeCustomerAddress(args.shippingAddress)
      : unnormalizeAddress(args.shippingAddress);

  const data = await query(
    context.api.setShippingAddressesOnCart({
      cart_id: cartId,
      shipping_addresses: [
        {
          address: address as CartAddressInput,
        },
      ],
    }),
  );

  return normalizeCart(data.setShippingAddressesOnCart?.cart as Cart);
});