close

Filter

Connector for Salesforce Commerce Cloud Manual / Version 2301

Table Of Contents

5.3 Extending the Shop Context

To render personalized or contextualized info in content areas it is important to have relevant shop context info available during CAE rendering. It will be most likely user session related info, that is available in the Commerce system only and must now be provided to the backend CAE. Examples are the user id of a logged in user, gender, the date the user was logged in the last time or the names of the customer groups the user belongs to, up to the info which campaign should be applied. Of course these are just examples and you can imagine much more. So it is important to have a place in order to extend the transferred shop context information flexibly.

The relevant shop context will be transmitted to the CoreMedia CAE automatically as HTTP header parameters and can there be accessed for using it as "personalization filter". It is a big advantage of the dynamic rendering of a CoreMedia CAE that you can easily process this information at rendering time.

The transmission of the context will be done automatically. You do not have to take care of it. On the one end, at the commerce system, there is a context provider script where the context info is gathered. To add custom information to the context please extend the prepared scripts/context/cmContextProvider.js script in the CoreMedia Cartridge for Salesforce. The exported functions in this script are called by the cmFragmentService when the context is built to pass it to the backend CAE. The packing, transmitting and unpacking of the values happen automatically.

Extending the ContextProvider

To extend the shop context you have to edit the cmContextProvider.js. There are three prepared exported function that are called by the cmFragmentService to build up the context information. By default, a base set of context information is already gathered and can be extended with custom values. Alternatively you can implement your own cmContextProvider by overwriting this module in your own customization cartridge and prepending it in the cartridge path.

Function Description

getPreviewContext

Gathers all preview related context information that should be sent as request headers. By default, an existing preview date is provided in the format 2018-04-17 18:30:00.000 and the associated timezone. The used request headers are: wc.preview.timestamp and wc.preview.timezone. This information is used by the Studio to render a preview assuming a certain date in the future.

getPreviewParams

Gathers all preview related context information that should be sent as request parameters. By default, an existing test persona (p13n_test and p13n_testcontext) and a preview date (timestamp) with its timezone (timezone). This information is used by the Studio to render a preview assuming a certain test persona (like Sarah or Matt) and a different time.

getUserContext

Gathers all user session related context information that should be sent as request headers. By default, current customer groups of a logged in user are provided. The used request header is: wc.user.membergroupids. This information is used by the CAE to personalize the rendering accordingly.

getUserParams

Gathers all user session related URL parameters. By default, this list is empty.

Table 5.3. Functions of the cmContextProvider.js script


Note

Note

The prefixes wc.preview and wc.user are automatically added by the connector and must not be provided as prefixes.

Caution

Caution

As a rough upper limit you should not exceed 4k bytes for all parameters, as they will be transmitted via HTTP headers. You should also note that this data must be transmitted with each backend call.

Read shop context values on the CAE side

On the backend CAE side the shop context values will be automatically provided via a Context API. You can access the context values during rendering via a Java API call.

All fragment requests are processed by the FragmentCommerceContextInterceptor in the CAE. This interceptor calls LiveContextContextAccessor.openAccessToContext(HttpServletRequest request) to create and store a Context object in the request. You can access the Context object via LiveContextContextHelper.fetchContext(HttpServletRequest request).

import com.coremedia.livecontext.fragment.links.context.Context;
import com.coremedia.livecontext.fragment.links.context.LiveContextContextHelper;

import javax.servlet.http.HttpServletRequest;

public class FragmentAccessExample {
...
  private LiveContextContextAccessor fragmentContextAccessor;

  public void buildContextHttpServletRequest request(){
    fragmentContextAccessor.openAccessToContext(request);
  }

  public String getUserIdFromRequest(HttpServletRequest request){
    Context context = LiveContextContextHelper.fetchContext(request);
    return (String) context.get("wc.user.id");
  }
...
}
      

Example 5.2. Access the Shop Context in CAE via Context API


Search Results

Table Of Contents
warning

Your Internet Explorer is no longer supported.

Please use Mozilla Firefox, Google Chrome, or Microsoft Edge.