Hooks in Salesforce Commerce Cloud (SFCC) allow developers to extend and customize storefront and data behavior at specific execution points. They are useful when functionality needs to execute conditionally based on system events or user actions. Hooks listen to defined extension points in the storefront or data layer and trigger custom logic only when required.
Types of Hooks in B2C Commerce
1. OCAPI Hooks
OCAPI provides predefined extension points that allow scripts to be executed before or after specific OCAPI requests. These are commonly used to customize behavior for system interactions such as customer updates or order modifications.
2. Custom Hooks
Custom hooks can be created and invoked in storefront code using the HookMgr class. These hooks allow flexible functionality sharing across multiple channels or applications that operate on the same site.
File Structure:-
Configuring OCAPI Hooks
To implement OCAPI hooks, ensure that your Business Manager instance has OCAPI permissions configured:
Business Manager → Administration → Site Development → Open Commerce API Settings
Your Shop API JSON configuration should include resources and permissions such as:
{
"_v":"19.10",
"clients":[
{
"client_id":"",
"resources":[
{
"resource_id":"/customers",
"methods":["post"],
"read_attributes":"(**)",
"write_attributes":"(**)"
},
{
"resource_id":"/customers/auth",
"methods":["post"],
"read_attributes":"(**)",
"write_attributes":"(**)"
},
{
"resource_id":"/customers/{customer_id}/addresses/{address_name}",
"methods":["get","patch"],
"read_attributes":"(**)",
"write_attributes":"(**)"
}
]
}
]
}
Defining Hooks in the Cartridge
Add the hooks configuration path in your cartridge's package.json:
{
"hooks": "./cartridge/scripts/hooks.json"
}
Example hooks.json:
{
"hooks": [
{
"name": "dw.ocapi.shop.customer.address.afterPATCH",
"script": "./cartridge/scripts/hooks/addressAfterPatch"
}
]
}
Hook Script Example
addressAfterPatch.js:
'use strict';
const Status = require('dw/system/Status');
const Transaction = require('dw/system/Transaction');
exports.afterPATCH = function (customer, addressName, customerAddress) {
Transaction.wrap(function () {
customerAddress.title = 'HOOK modified Title';
});
return new Status(Status.OK);
}
This hook triggers after a customer address PATCH request and updates the address title.
Data API Hooks
Data API hooks function similarly but operate on resources in the Data API. For example, modifying customer data stored in customer lists requires configuration under Data API in OCAPI settings.
Summary
Hooks extend platform functionality by enabling custom logic at defined execution points. Combined with OCAPI, they allow controlled external system interactions with SFCC data and storefront processes. Understanding hooks ensures efficient customization aligned with client requirements
End Note
Hooks offer a structured way to extend SFCC functionality, and when used alongside OCAPI, they enable smooth and secure external interactions. As a next step, exploring how caching works in SFCC will help you further optimize performance and storefront efficiency.
Have any questions? Feel free to drop an email to support@astreait.com or visit astreait.com to schedule a consultation.