Order normalizer
normalizeOrder
: This function is used to map BigCommerceOrderByCartResponse
intoSfOrder
, which includes order details data.normalizeOrderListItem
: This function maps BigCommerceOrder
into UnifiedSfOrderListItem
which includes only basic order details, used to display an data in an order list.
Parameters
normalizeOrder
Name | Type | Default value | Description |
---|---|---|---|
context | NormalizerContext | Context which contains e.g. currency | |
input | OrderByCartResponse | BigCommerce OrderByCartResponse |
normalizeOrderListItem
Name | Type | Default value | Description |
---|---|---|---|
context | NormalizerContext | Context which contains e.g. currency | |
order | Order | BigCommerce Order |
normalizeOrderLineItem
Name | Type | Default value | Description |
---|---|---|---|
context | NormalizerContext | Context which contains e.g. currency | |
lineItem | OrderItem | BigCommerce 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",
};
});