Search code examples
demandwaresalesforce-commerce-cloud

Create session redirect link in content asset


Our company has multiple brands and each brand has its own host name, but they are all part of the same site. We can let customers share baskets and other session information when they switch between brands via a redirect link using URLUtils.sessionRedirect.

But URLUtils is not available in content assets. Is it possible to form a session redirect link in content asset keeping all the session information?

Thanks in advance.


Solution

  • You can include dynamic content in Content Assets with the $include('Controller-Name', 'name1', 'value1', 'name2', 'value2', ...)$ syntax. See the MarkupText Class Documentation for more info on that syntax. The 'name1' and 'value1' parameters are mapped as query string attributes eg: Controller-Name?name1=value1&name2=value2

    Create a controller that outputs the session redirect link you need, and call it via that syntax like: $include(Util-RenderSessionLink, 'siteID', 'foo')$

    The controller would need to use a response Content-Type header of text/plain or something like that so that nothing is injected into the response. (eg: Storefront toolkit or tracking tags) For example:

    response.setContentType('text/plain');
    

    Alternatively, you could process the content asset for some sorts of keys that you perform find & replace operations on. For example, the following code does a find & replace on a Content Asset's body content for the key: '%%SessionLink%%'.

    var ContentMgr = require('dw/content/ContentMgr');
    var URLUtils = require('dw/web/URLUtils');
    
    if (!empty(content) {
      var content = ContentMgr.getContent('my-content-id');
      var contentOut = "";
      var viewData = {};
      
      contentOut = content.custom.body.getMarkup()
         .replace('%%SessionLink%%', URLUtils.sessionRedirect(...));
      
      viewData.content = contentOut;
      // then output your `pdict.content` key within a template with the appropriate encoding
    }