Skip to content

Commit e9c1b23

Browse files
committed
refine OCAPI scapi hooks skill
1 parent 1fd40de commit e9c1b23

2 files changed

Lines changed: 393 additions & 304 deletions

File tree

skills/b2c/skills/b2c-hooks/SKILL.md

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ var Status = require('dw/system/Status');
7777

7878
exports.afterPOST = function(basket) {
7979
// Called after basket creation
80-
return new Status(Status.OK);
80+
// Returning a value would skip system implementation
8181
};
8282

8383
exports.modifyPOSTResponse = function(basket, basketResponse) {
@@ -128,6 +128,46 @@ return status;
128128
| `Status.ERROR` | 400 Bad Request | Transaction rolled back, processing stops |
129129
| Uncaught exception | 500 Internal Error | Transaction rolled back |
130130

131+
## Return Value Behavior (Important)
132+
133+
**OCAPI/SCAPI hooks that return ANY value will SKIP the system implementation and all subsequent registered hooks for that extension point.**
134+
135+
This is a common source of bugs. For example, if a hook returns `Status.OK`, the system's `dw.order.calculate` implementation won't run, causing cart totals to be incorrect.
136+
137+
### When to Return a Value
138+
139+
Return a `Status` object **only** when you want to:
140+
- **Stop processing** with an error (`Status.ERROR`)
141+
- **Skip the system implementation** intentionally
142+
143+
### When NOT to Return a Value
144+
145+
To ensure system implementations run (like cart calculation), **return nothing**:
146+
147+
```javascript
148+
// Returning Status.OK skips system implementation
149+
exports.afterPOST = function(basket) {
150+
doSomething(basket);
151+
return new Status(Status.OK); // Skips dw.order.calculate
152+
};
153+
154+
// No return value - system implementation runs
155+
exports.afterPOST = function(basket) {
156+
doSomething(basket);
157+
// No return, or explicit: return;
158+
};
159+
```
160+
161+
### Summary
162+
163+
| Return Value | OCAPI/SCAPI Behavior | Custom Hook Behavior |
164+
|-------------|---------------------|---------------------|
165+
| `undefined` (no return) | System implementation runs, subsequent hooks run | All hooks run |
166+
| `Status.OK` | **Skips** system implementation and subsequent hooks | All hooks run |
167+
| `Status.ERROR` | Stops processing, returns error | All hooks run |
168+
169+
**Debugging tip**: If cart totals are wrong or hooks aren't firing, check if an earlier hook is returning a value.
170+
131171
## OCAPI/SCAPI Hooks
132172

133173
OCAPI and SCAPI share the same hooks. Enable in Business Manager:
@@ -157,7 +197,7 @@ exports.beforePUT = function(basket, addressDoc) {
157197
exports.afterPOST = function(basket, paymentDoc) {
158198
var result = callPaymentService(paymentDoc);
159199
request.custom.paymentResult = result; // Pass to modifyResponse
160-
return new Status(Status.OK);
200+
// Returning a Status would skip system implementation
161201
};
162202

163203
// Modify response
@@ -305,20 +345,15 @@ exports.modifyGETResponse = function(product, doc) {
305345

306346
## Best Practices
307347

308-
### Do
309-
310-
- Return `Status` objects to control flow
348+
- Return `undefined` (no return) from OCAPI/SCAPI hooks to ensure system implementations run
349+
- Only return `Status.ERROR` when you need to stop processing
350+
- Returning `Status.OK` skips system implementation and subsequent hooks
311351
- Use `request.custom` to pass data between hooks
312352
- Check `request.isSCAPI()` when supporting both APIs
313353
- Keep hooks focused and performant
314354
- Use custom properties (`c_` prefix) in modifyResponse
315-
316-
### Don't
317-
318-
- Use transactions in calculate hooks (breaks SCAPI)
319-
- Modify standard response properties (only `c_` properties)
320-
- Rely on hook execution order across cartridges
321-
- Make slow external calls in beforeGET (affects caching)
355+
- Avoid transactions in calculate hooks (breaks SCAPI)
356+
- Avoid slow external calls in beforeGET (affects caching)
322357

323358
## Error Handling
324359

0 commit comments

Comments
 (0)