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
inputOrderMagento Order

normalizeOrderListItem

NameTypeDefault valueDescription
orderOrderMagento Order

normalizeOrderLineItem

NameTypeDefault valueDescription
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 defineNormalizers function. The following example demonstrates how to extend SfOrder with an invoices field.

import { normalizers as normalizersMagento, defineNormalizers } from "@vsf-enterprise/unified-api-magento";

const normalizers = defineNormalizers<typeof normalizersMagento>()({
  ...normalizersMagento,
  normalizeOrder: (order, context) => ({
    ...normalizersMagento.normalizeOrder(order, context),
    invoices: order.invoices,
  }),
});

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((input, ctx) => {
  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 } =
    ctx.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),
  };
});