Vue Storefront is now Alokai! Learn More
Order normalizer

Order normalizer

  • normalizeOrder: This function is used to map Magento Order into SfOrder, which includes order details data.
  • normalizeOrderListItem: This function maps Magento OrderHistory into Unified 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
inputOrderMagento Order

normalizeOrderListItem

NameTypeDefault valueDescription
contextNormalizerContextcontext needed for the normalizer
orderOrderMagento Order

normalizeOrderLineItem

NameTypeDefault valueDescription
contextNormalizerContextcontext needed for the normalizer
lineItemOrderItemInterfaceMagento Order Item

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 an invoices field.

export const unifiedApiExtension = createUnifiedExtension({
  normalizers: {
    addCustomFields: [
      {
        normalizeOrder: (context, order) => ({
          invoices: order.invoices,
        }),
      },
    ],
  },
  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 {
    id,
    items,
    order_date,
    shipping_address,
    shipping_method,
    total,
    payment_methods,
    billing_address,
  } = input;
  if (
    !id ||
    !order_date ||
    !total ||
    !total?.shipping_handling?.total_amount ||
    !total.total_tax ||
    !total.subtotal ||
    !total.grand_total ||
    !items ||
    items.length === 0 ||
    !shipping_address ||
    !shipping_method ||
    !payment_methods ||
    !payment_methods[0] ||
    !shipping_address
  ) {
    throw new Error("Missing required order fields");
  }
  const { normalizeOrderLineItem, normalizeAddress, normalizeShippingMethod, normalizeMoney } =
    context.normalizers;

  return {
    id,
    lineItems: items.filter(Boolean).map((item) => normalizeOrderLineItem(item)) ?? [],
    orderDate: order_date,
    status: input.status ?? "UNKNOWN",
    billingAddress: billing_address ? normalizeAddress(billing_address) : null,
    shippingAddress: normalizeAddress(shipping_address),
    paymentMethod: payment_methods[0].name,
    shippingMethod: normalizeShippingMethod({
      carrier_code: shipping_method,
      amount: total.shipping_handling.total_amount,
      method_code: shipping_method,
      method_title: shipping_method,
    }),
    subtotalPrice: normalizeMoney(total.subtotal),
    totalPrice: normalizeMoney(total.grand_total),
    totalShippingPrice: normalizeMoney(total.shipping_handling?.total_amount),
    totalTax: normalizeMoney(total.total_tax),
  };
});