Hooks in SFCC is a CommonJS script module. Hooks can be
configured as a piece of functionality to use it in a specific point in your
application flow or at a specific event.
You can use these hooks with a Salesforce 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 System package HookMgr class 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.
Hook Definition
The package.json file will have the hook file entry for a
cartridge. The hook file definitions shown as below
{
"hooks": "./hooks.json"
}
The hook
file defines a uniquely named extension point and a script to run. As the hook
scripts are 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 Example
Hook Example
The hook
.json
file defines a dw.ocapi.shop.basket.calculate hook that calls the calculate.js script. These hooks are located subdirectories
of the scripts/hooks directory
{
"hooks": [
{
"name": "dw.order.calculate",
"script": "./cartridge/scripts/hooks/cart/calculate.js"
},
{
"name": "dw.order.calculateShipping",
"script": "./cartridge/scripts/hooks/cart/calculate.js"
},
{
"name": "dw.order.calculateTax",
"script": "./cartridge/scripts/hooks/cart/calculate.js"
},
{
"name": "app.payment.processor.default",
"script": "./cartridge/scripts/hooks/payment/processor/default"
},
{
"name": "app.payment.processor.basic_credit",
"script": "./cartridge/scripts/hooks/payment/processor/basic_credit"
}
]
}
These hooks will be loaded during cartridge initialization and
will be ready to be called in storefront module’s code.
Calling Custom Hook
Calling Custom Hook
{
"name": "dw.order.calculate",
"script": "./cartridge/scripts/hooks/cart/calculate.js"
}
This example calls the hook
from calculate.js.
Sample code from storefront module. The code calls the hook
dw.order.calculate’s calculate method by sending basket as a parameter in calculate.js
var HookMgr = require('dw/system/HookMgr');
function calculateTotals(basket) {
HookMgr.callHook('dw.order.calculate', 'calculate', basket);
}
Running Multiple Hooks for an Extension Point
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.
Note: 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.
Error Logging
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.
Example: Custom error log
Error while executing script 'test_cartridge_treatascustom/cartridge/controllers/TestController.js': Wrapped com.demandware.beehive.core.internal.template.ServletAbortException: Requested template 'controller/testController' not found! (test_cartridge_treatascustom/cartridge/controllers/TestController.js#21)
at test_cartridge_treatascustom/cartridge/controllers/TestController.js:21 (isml)
at test_cartridge_treatascustom/cartridge/controllers/TestController.js:52 (anonymous)
Comments
Post a Comment