Magento 2.x - Serverside

An outline of serverside mode, and how to customise templates & extend the data shown

Peter Brooksbank avatar
Written by Peter Brooksbank
Updated over a week ago

In "Serverside" mode requests for recommenders are routed through Magento to ensure that recommenders are populated using Magento data after getting SKUs to show from PureClarity. This is ideal for when you have complex and/or custom pricing rules & logic or non-standard restrictions on what products customers can see.

This means there are a couple of differences in how you manage PureClarity recommender templates and an extra extension point for adding/modifying your own data in templates.

Recommender template

Product Recommender template HTML is managed via a "knockout" template in the extension and not via the template editor in PureClarity Admin.

This means any changes you make to product recommender templates in the Template Editor or Recommender designer in PureClarity Admin will not be reflected in the frontend in serverside mode.

Product recommenders will inherit any styling from your theme in Magento and not have it's own unless you add your own styles in your Magento theme.

To customise the recommender template you must copy the following template to the relevant location in your theme, in order to override it:

vendor/pureclarity/pureclarity-magento-2/view/frontend/web/template/product-recommender.html

Changing Product Data

You can intercept any product before it is rendered in the template by hooking into the serverside processing that pulls data from Magento. This is useful if you have custom pricing that resides outside of the normal Magento flow or want to add addtional custom fields to the data.

The Class that handles the serverside response is:

\Pureclarity\Core\Model\Serverside\Response\ProductData

In this class, the populateProductData function is responsible for adding the data to the array that gets passed to knockout to populate the template.

To add additional data, we recommend adding an after plugin to process extra data and add it to the data sent to knockout.

di.xml

<config>
<type name="">
<plugin name="\Pureclarity\Core\Model\Serverside\Response\ProductData" type="\My\Module\Plugin\PureClarity\ProductDataPlugin" sortOrder="1" disabled="false" />
</type>
</config>

RowDataPlugin.php

namespace My\Module\Plugin\PureClarity;

use \Pureclarity\Core\Model\Serverside\Response\ProductData;

class ProductDataPlugin
{
/**
* @param ProductData $subject
* @param mixed[] $data -is the row of data for a product, to be sent to knockout for use in the template
* @return mixed
*/
public function afterPopulateProductData(ProductData $subject, array $data)
{
$data['price'] = <price returned by some other method of calculating price>;
$data['my_custom_field'] = 'Some text';

return $data;
}
}

If you added a custom field, you can then reference it in the product recommender template html like this:

<span class="price" data-bind="text: my_custom_field"></span>

Knockout will then render the data.

Did this answer your question?