January 29, 2019

With the new Salesforce Reference Architecture (SFRA) you are encouraged to keep the app_storefront_base untouched. Instead, custom functionality can be added by extending existing code. Let’s have a look how to do this with the Product Controller:

This controller should be named Product.js (as the one it is extending) in your custom cartridge.

The server script (located in modules/server/server.js) is doing the magic here. It provides methods for adding new endpoints, or replacing and modifying existing ones.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
'use strict';

var account = module.superModule;
var server = require('server');

server.extend(account);

server.append('Show', function (req, res, next) {
  var product = res.viewData.product;
  if (product.productType === 'master') {
    res.setViewData({
      renderProductOverview: true
    });
  }
  next();
});

module.exports = server.exports();

In this example, the server.append(…​) method will be executed after the regular Product-Show logic and add a new variable to the pipeline dictionary, without changing any of the existing logic. With the call to next() any further logic will be executed - if needed, this could be avoided by not calling it.

Contact