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 "@vue-storefront/magento-types";
import { getNormalizers } from "@vue-storefront/unified-data-model";

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);
});