Vue Storefront is now Alokai! Learn More
Order normalizer

Order normalizer

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

Parameters

normalizeOrder

NameTypeDefault valueDescription
contextNormalizerContextContext which contains e.g. currency
inputOrderByCartResponseBigCommerce OrderByCartResponse

normalizeOrderListItem

NameTypeDefault valueDescription
contextNormalizerContextContext which contains e.g. currency
orderOrderBigCommerce Order

normalizeOrderLineItem

NameTypeDefault valueDescription
contextNormalizerContextContext which contains e.g. currency
lineItemOrderItemBigCommerce 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 a staffNotes field.

export const unifiedApiExtension = createUnifiedExtension({
  normalizers: {
    addCustomFields: [
      {
        normalizeOrder: (context, order) => ({
          staffNotes: order.staff_notes,
        }),
      },
    ],
  },
  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,
    date_created,
    status,
    products,
    subtotal_ex_tax,
    shipping_cost_inc_tax,
    total_tax,
    total_inc_tax,
    billing_address,
    shipping_addresses: { 0: shipping_address },
  } = input;
  const { shipping_method } = shipping_address ?? {};

  if (
    !date_created ||
    !subtotal_ex_tax ||
    !shipping_cost_inc_tax ||
    !total_tax ||
    !total_inc_tax ||
    !billing_address ||
    !products ||
    !Array.isArray(products) ||
    !shipping_method ||
    !shipping_address
  ) {
    throw new Error("Missing required order fields");
  }

  const lineItems = products.map((entry) => context.normalizers.normalizeOrderLineItem(entry));

  return {
    id: id.toString(),
    orderDate: new Date(date_created).toISOString(),
    status: status ?? "UNKNOWN",
    lineItems,
    subtotalPrice: context.normalizers.normalizeMoney(Number.parseFloat(subtotal_ex_tax)),
    totalShippingPrice: context.normalizers.normalizeMoney(
      Number.parseFloat(shipping_cost_inc_tax),
    ),
    totalTax: context.normalizers.normalizeMoney(Number.parseFloat(total_tax)),
    totalPrice: context.normalizers.normalizeMoney(Number.parseFloat(total_inc_tax)),
    shippingAddress: context.normalizers.normalizeAddress(shipping_address),
    billingAddress: context.normalizers.normalizeAddress(billing_address),
    shippingMethod: context.normalizers.normalizeShippingMethod({
      name: shipping_method,
      price: shipping_cost_inc_tax,
    }),
    paymentMethod: "CARD",
  };
});