Order normalizer
normalizeOrder
: This function is used to map MagentoOrder
intoSfOrder
, which includes order details data.normalizeOrderListItem
: This function maps MagentoOrderHistory
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 needed for the normalizer | |
input | Order | Magento Order |
normalizeOrderListItem
Name | Type | Default value | Description |
---|---|---|---|
context | NormalizerContext | context needed for the normalizer | |
order | Order | Magento Order |
normalizeOrderLineItem
Name | Type | Default value | Description |
---|---|---|---|
context | NormalizerContext | context needed for the normalizer | |
lineItem | OrderItemInterface | Magento 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
/* eslint-disable complexity */
import { defineNormalizer } from "../defineNormalizer";
export const normalizeOrder = defineNormalizer.normalizeOrder((context, input) => {
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 ||
!Array.isArray(items) ||
!shipping_address ||
!shipping_method ||
!payment_methods ||
!payment_methods[0] ||
!shipping_address
) {
throw new Error("Missing required order fields");
}
const { normalizeOrderLineItem, normalizeAddress, normalizeShippingMethod, normalizeMoney } =
context.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),
};
});