Class ShippingHooks
This interface represents shipping extension points for Commerce App shipping providers.
These hooks provide integration points for external shipping rate / delivery-estimate services installed via the Commerce App framework.
IMPORTANT: These hooks should only be implemented and registered by Commerce Apps
(applications installed via the Commerce App framework with a CAP file). They are not intended for custom merchant
cartridges or storefront implementations. Merchants who want custom shipping calculation logic should use the legacy
dw.order.calculateShipping extension point instead.
Hook Registration A function must be defined inside a JavaScript source and must be exported. The script
with the exported hook function must be located inside a site cartridge. Inside the site cartridge a
package.json file with a ‘hooks’ entry must exist:
The hooks entry links to a JSON file, relative to the package.json file. This file lists all registered
hooks inside the hooks property:
A hook entry has a name and a script property:
- The
namecontains the extension point name (the hook name). - The
scriptcontains the script path relative to the hooks file, with the exported hook function.
Function Naming Convention: The exported JavaScript function name must match the last segment of the
extension point name, for example, quote or calculate.
Hook Lifecycle Each hook fires at a specific surface and lifecycle stage:
- quote(checkout): Fires on every invocation of
GET /baskets/{basket_id}/shipments/{shipment_id}/shipping_methods. Lets the Commerce App override native shipping prices and add delivery information for the methods it can quote. - calculate(basket calculation): Fires during basket calculation on every basket operation that recomputes shipping. Applies provider-supplied shipping rates to the selected
ShippingLineItemand may persist provider metadata on the shipment.
Hook Precedence
- When
sfcc.app.shipping.calculateis registered and theShippingAppHooksEnabledtoggle is on, it takes precedence over the legacydw.order.calculateShippinghook. - If the Commerce App hook is not registered (or the toggle is off), the platform falls back to
dw.order.calculateShipping(if registered). - If neither hook is available, the platform uses the native default (
ShippingMgr.applyShippingCost).
| Constant | Description |
|---|---|
| SHIPPING_DOMAIN: String = “shipping” | The shipping app domain segment used to compose extension-point names under the shared sfcc.app prefix. |
| extensionPointCalculate: String = “sfcc.app.shipping.calculate” | The extension point name sfcc.app.shipping.calculate. |
| extensionPointQuote: String = “sfcc.app.shipping.quote” | The extension point name sfcc.app.shipping.quote. |
This class does not have a constructor, so you cannot create it directly.
| Method | Description |
|---|---|
| calculate(LineItemCtnr) | The function is called by extension point extensionPointCalculate during basket calculation. |
| quote(Shipment, ShippingMethodResultWO) | The function is called by extension point extensionPointQuote on every invocation of GET /baskets/{basket_id}/shipments/{shipment_id}/shipping_methods. |
assign, create, create, defineProperties, defineProperty, entries, freeze, fromEntries, getOwnPropertyDescriptor, getOwnPropertyNames, getOwnPropertySymbols, getPrototypeOf, hasOwnProperty, is, isExtensible, isFrozen, isPrototypeOf, isSealed, keys, preventExtensions, propertyIsEnumerable, seal, setPrototypeOf, toLocaleString, toString, valueOf, values
- SHIPPING_DOMAIN: String = "shipping"
The shipping app domain segment used to compose extension-point names under the shared
sfcc.appprefix.
- extensionPointCalculate: String = "sfcc.app.shipping.calculate"
The extension point name sfcc.app.shipping.calculate.
- extensionPointQuote: String = "sfcc.app.shipping.quote"
The extension point name sfcc.app.shipping.quote.
- calculate(lineItemCtnr: LineItemCtnr): Status
The function is called by extension point extensionPointCalculate during basket calculation. It applies provider-supplied shipping rates to the selected
ShippingLineItemand may persist provider metadata (for example, a rate id, carrier code, or delivery window) on the shipment via supportedcustom.*attributes. This hook fires on every basket operation that recomputes shipping, not only before order creation.The hook owns: native fallback (calling
ShippingMgr.applyShippingCost(lineItemCtnr)if it wants to preserve product-level shipping, surcharges, cleanup, and tax-class setup), provider lookup / rate-cache reuse, selected-rate application onto the standard shipment shipping line item, and provider metadata persistence (custom attributes on the shipment, custom objects, etc.).Note: If the master
dw.order.calculatehook (not the per-stepdw.order.calculateShipping) is overridden, the entire basket calculation is replaced and the platform's automatic shipping hook selection is bypassed. You must manually invoke this hook from within your customdw.order.calculateimplementation if you want to use Commerce App shipping providers.Error Handling: Both returning a
Status.ERRORand throwing an exception will prevent the basket calculation from completing successfully. The platform logs the error and halts the current basket operation. Since order creation requires a successful basket calculation, this also prevents orders from being created with incorrect shipping amounts.Sample Implementation:
Parameters:
- lineItemCtnr - the line item container (basket) for which shipping should be calculated.
Returns:
Status.OKornullfor success;Status.ERRORto block the basket calculation with details about the failure. Throwing an exception will also block the basket calculation.
- quote(shipment: Shipment, result: ShippingMethodResultWO): Status
The function is called by extension point extensionPointQuote on every invocation of
GET /baskets/{basket_id}/shipments/{shipment_id}/shipping_methods. ECOM pre-populates aShippingMethodResultWOwith the applicable shipping methods, each carrying its native price. The hook implementation may override prices and delivery information on any methods it can quote. Methods the hook does not touch keep their native price.Error Handling: To signal a failure, return
new Status(Status.ERROR). The platform logs that an error status was returned for this hook and theGET shipping-methodsrequest fails. Uncaught exceptions thrown from the hook are also treated as failures.SCAPI Behavior: When
ScapiHookExecutionEnabledis disabled, SCAPI requests bypass this hook and use native shipping pricing.Sample Implementation:
Parameters:
- shipment - the shipment for which shipping methods are being displayed.
- result - the pre-populated result with native prices on each method. Mutated in place by the hook.
Returns:
Status.OKornullfor success;Status.ERRORto block the request.