Order normalizer
normalizeOrder
: This function is used to map SFCCOrder
intoSfOrder
, which includes order details data.normalizeOrderListItem
: This function maps SFCCProductItem
intoSfOrderListItem
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 needed for the normalizer. Context contain a currency field that contains a currency code | |
input | Order | SFCC Order |
normalizeOrderListItem
Name | Type | Default value | Description |
---|---|---|---|
context | NormalizerContext | Context needed for the normalizer. | |
input | ProductItem | SFCC Order List Element |
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 shipments
field.
export const unifiedApiExtension = createUnifiedExtension({
normalizers: {
addCustomFields: [
{
normalizeOrder: (context, order) => ({
shipments: order.shipments,
}),
},
],
},
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 {
orderNo,
creationDate,
status,
productItems,
productTotal,
taxTotal,
shippingTotal,
billingAddress,
shipments,
} = input;
if (
!orderNo ||
!creationDate ||
!productItems ||
!Array.isArray(productItems) ||
!productTotal ||
!taxTotal ||
shippingTotal === undefined ||
shippingTotal < 0 ||
!billingAddress ||
!shipments ||
shipments?.length === 0 ||
!("shippingMethod" in shipments[0]!) ||
!("shippingAddress" in shipments[0]!)
) {
throw new Error("Missing required order fields");
}
const subTotalWithoutTax = productTotal - taxTotal;
const { shippingAddress, shippingMethod } = shipments[0];
const { normalizeMoney, normalizeShippingMethod, normalizeAddress, normalizeOrderLineItem } =
context.normalizers;
return {
id: orderNo,
orderDate: new Date(creationDate).toISOString(),
status: status ?? "UNKNOWN",
lineItems: productItems.map((entry) => normalizeOrderLineItem(entry)),
subtotalPrice: normalizeMoney(subTotalWithoutTax),
totalShippingPrice: normalizeMoney(shippingTotal),
totalTax: normalizeMoney(taxTotal),
totalPrice: normalizeMoney(productTotal),
shippingAddress: normalizeAddress(shippingAddress!),
billingAddress: normalizeAddress(billingAddress),
shippingMethod: normalizeShippingMethod(shippingMethod!)!,
paymentMethod: "CARD",
};
});