Vue Storefront is now Alokai! Learn More
Order normalizer

Order normalizer

  • normalizeOrder: This function is used to map SFCC Order into SfOrder, which includes order details data.
  • normalizeOrderListItem: This function maps SFCC ProductItem into SfOrderListItem which includes only basic order details, used to display an data in an order list.

Parameters

normalizeOrder

NameTypeDefault valueDescription
contextNormalizerContextContext needed for the normalizer. Context contain a currency field that contains a currency code
inputOrderSFCC Order

normalizeOrderListItem

NameTypeDefault valueDescription
contextNormalizerContextContext needed for the normalizer.
inputProductItemSFCC Order List Element

Extending

The SfOrder is returned from the GetOrders Method. If the SfOrder structure doesn't contain the information you need for your Storefront, you can extend its logic using the addCustomFields API. The following example demonstrates how to extend SfOrder with a shipments field.

export const unifiedApiExtension = createUnifiedExtension({
  normalizers: {
    addCustomFields: [
      {
        normalizeOrder: (context, order) => ({
          shipments: order.shipments,
        }),
      },
    ],
  },
  config: {
    ...
  },
});

You can override the normalizeOrder, but it's also available to override the smaller normalizers such as normalizeAddress, normalizeShippingMethod.

Source

order.ts
/* eslint-disable complexity */
import { defineNormalizer } from "../defineNormalizer";

export const normalizeOrder = defineNormalizer.normalizeOrder((context, input) => {
  const {
    orderNo,
    creationDate,
    status,
    productItems,
    productTotal,
    taxTotal,
    shippingTotal,
    billingAddress,
    shipments,
  } = input;

  if (
    !orderNo ||
    !creationDate ||
    !productItems ||
    productItems?.length === 0 ||
    !productTotal ||
    !taxTotal ||
    shippingTotal === undefined ||
    shippingTotal < 0 ||
    !billingAddress ||
    !shipments ||
    shipments?.length === 0 ||
    !("shippingMethod" in shipments[0]!) ||
    !("shippingAddress" in shipments[0]!)
  ) {
    throw new Error("Missing required order fields");
  }

  const subTotalWithoutTax = productTotal - taxTotal;
  const { shippingAddress, shippingMethod } = shipments[0];
  const { normalizeMoney, normalizeShippingMethod, normalizeAddress, normalizeOrderLineItem } =
    context.normalizers;

  return {
    id: orderNo,
    orderDate: new Date(creationDate).toISOString(),
    status: status ?? "UNKNOWN",
    lineItems: productItems.map((entry) => normalizeOrderLineItem(entry)),
    subtotalPrice: normalizeMoney(subTotalWithoutTax),
    totalShippingPrice: normalizeMoney(shippingTotal),
    totalTax: normalizeMoney(taxTotal),
    totalPrice: normalizeMoney(productTotal),
    shippingAddress: normalizeAddress(shippingAddress!),
    billingAddress: normalizeAddress(billingAddress),
    shippingMethod: normalizeShippingMethod(shippingMethod!)!,
    paymentMethod: "CARD",
  };
});