Cert++
Close

System extension points for basket, order and request events

Key Takeaway

If you write your own dw.order.calculate hook, call dw.order.calculateShipping and dw.order.calculateTax from inside it through HookMgr, because your override bypasses the platform's automatic tax hook selection.

A hook links a named extension point, such as dw.order.calculate, to your own script. B2C Commerce calls the script at a set moment, for example when a basket is calculated, when an order number is created, or when a storefront request or session starts. You register the hook in a cartridge's hooks file. When the event happens, the platform runs your exported function. You use hooks to customize built-in logic or to react to an event.

How to register a hook

A hook needs three things in a site cartridge: your script, a package.json file and a hooks JSON file.

  1. Write the hook script as a JavaScript source file inside a site cartridge. Implement it as a CommonJS module that exports the hook function, for example calculate.
  2. Add a package.json file to the site cartridge with a "hooks" entry. The entry points to your hooks JSON file with a path relative to package.json, for example "hooks": "./cartridge/scripts/hooks.json".
  3. In the hooks JSON file, add an entry to the "hooks" array. Each entry has a "name" (the extension point) and a "script" (the script path, relative to the hooks file).
{
  "hooks": [
    { "name": "dw.order.calculate", "script": "./calculate.js" },
    { "name": "dw.system.request.onSession", "script": "./script.js" },
    { "name": "dw.system.request.onRequest", "script": "./script.js" }
  ]
}

Each entry maps one extension point to one script. Script paths are relative to the hooks file, so ./calculate.js sits in the same folder as hooks.json. Two extension points can share a script: here dw.system.request.onSession and dw.system.request.onRequest both use ./script.js.

B2C Commerce Developer Study Guide

Checklist progress

0/237Learned

Given a use case, extend functionality or capture an event using hook extension points

0/6

Prepare for the Exam

Play Today's Certle

Study Community

Ask questions and get the latest info from other B2C Commerce Developer studiers. 796 members and growing.