Use hooks to configure functionality to be called at a specific point in your application flow or at a specific event.
You can use these hooks with a B2C Commerce storefront application:
- OCAPI hooks: B2C Commerce provides extension points to call scripts before or after specific OCAPI calls.
- Custom hooks: You can define custom extension points and call them in your storefront code using the B2C Commerce script
HookMgrclass methods. You can then access the hook in either OCAPI or your storefront code. This flexibility makes them useful for functionality in a multichannel set of applications based on the same site.
package.json file points to the hook file for a cartridge, using the
The hook file defines a uniquely named extension point and a script to run. You can implement hook scripts as a CommonJS module. This approach ensures that the
script identifier is a module identifier. The value of this identifier can be a relative path or any other valid module identifier.
hook.json file defines a
dw.ocapi.shop.basket.calculatehook that calls the
This example shows an OCAPI hook and several custom hooks. The OCAPI hook runs a script to calculate the cart in the
scripts/hooks/cart directory. If you use the System package
callHook method, you can call the custom hooks. These hooks are located subdirectories of the
This example calls the hook from calculate.js.
In a single
hooks.json file, you can register multiple modules to call for an extension point. However, you can't control the order in which the modules are called. If you call multiple modules, only the last hook returns a value. All modules are called, regardless of whether any of them return a value.
At run time, B2C Commerce runs all hooks registered for an extension point in all cartridges in your cartridge path. Hooks are executed in the order their cartridges appear on the path. Each cartridge can register a module for the same hook. Modules are called in cartridge-path order for all cartridges in which they are registered.
Hooks are executed in the order of the cartridges on the path. Therefore, when you change the order of the cartridges, you also change the order of hook execution.
Controller and script logging is available.
- Custom error log: Contains the hierarchy of controller and script functions and the line numbers related to exceptions thrown. Intended for developers to debug code.
- System error log: Primarily used for Commerce Cloud Support.