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 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
import { ValidationError } from "@alokai/connect/middleware";
import { z } from "zod";

import { defineNormalizer } from "../defineNormalizer";

const orderSchema = z.looseObject({
  id: z.string(),
  items: z.array(z.any()),
  order_date: z.string(),
  payment_methods: z.array(z.looseObject({ name: z.string() })).min(1),
  shipping_address: z.looseObject({}),
  shipping_method: z.string(),
  total: z.looseObject({
    grand_total: z.looseObject({}),
    shipping_handling: z.looseObject({
      total_amount: z.looseObject({}),
    }),
    subtotal: z.looseObject({}),
    total_tax: z.looseObject({}),
  }),
});

export const normalizeOrder = defineNormalizer.normalizeOrder((context, input) => {
  const result = orderSchema.safeParse(input);
  if (!result.success) {
    throw ValidationError.fromStandardSchemaError(result.error, "Missing required order fields");
  }
  const validated = result.data as typeof input & typeof result.data;
  const {
    billing_address,
    id,
    items,
    order_date,
    payment_methods,
    shipping_address,
    shipping_method,
    total,
  } = validated;
  const { normalizeAddress, normalizeMoney, normalizeOrderLineItem, normalizeShippingMethod } =
    context.normalizers;

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