Skip to content

Overriding Product Data

Mridang Agarwalla edited this page Jan 4, 2018 · 14 revisions

In order to modify the product data that is sent to Nosto, you need to create your own mini-module. Why a new module? Because in addition to the product meta-data that is added to your shops frontend, Nosto also uses a server-to-server API to send product information when products are updated in the PrestaShop backend. This is done to always keep the recommendations up-to-date with the newest information. If you would only override the template file for the meta-data in the frontend, the changes would not affect the API calls form the backend, which would cause Nosto to get conflicting information about the same products.

Note: you need version 2.5.0 or higher of the Nosto module for this to work.

The module requires only 2 files to work. Below you can find an example module which you can simply copy&paste into your PrestaShop modules folder inside your installation. Let's call the module My Nosto. First create a folder, mynosto, inside your PrestaShop modules folder. Then copy paste these 2 files below into that new folder.

Note: if you cannot make changes directly inside your PrestaShop installation, you can create the mynosto folder locally on your own computer and put the files inside it, after which you create a zip archive of the whole folder that you can then upload in your PrestaShop backend like any other module.

File config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<module>
    <name>mynosto</name>
    <displayName><![CDATA[My Nosto]]></displayName>
    <version><![CDATA[0.1.0]]></version>
    <description><![CDATA[Module for overriding Nosto default behavior]]></description>
    <author><![CDATA[Nosto]]></author>
    <tab><![CDATA[advertising_marketing]]></tab>
    <is_configurable>0</is_configurable>
    <need_instance>1</need_instance>
    <limited_countries></limited_countries>
</module>

File mynosto.php

<?php

if (!defined('_PS_VERSION_')) exit;

/**
 * Module for overriding Nosto default behavior.
 *
 * Depends on the "Personalization for PrestaShop" module by Nosto.
 */
class MyNosto extends Module

{
    /**
     * Constructor.
     *
     * Defines the module.
     */
    public

    function __construct()
    {
        $this->name = 'mynosto';
        $this->tab = 'advertising_marketing';
        $this->version = '0.1.0';
        $this->author = 'Nosto';
        $this->need_instance = 1;
        $this->bootstrap = true;
        $this->ps_versions_compliancy = array(
            'min' => '1.4',
            'max' => _PS_VERSION_
        );
        parent::__construct();
        $this->displayName = $this->l('My Nosto');
        $this->description = $this->l('Module for overriding Nosto default behavior');
    }

    /**
     * Module installer.
     *
     * Registers the `actionNostoProductLoadAfter` in order to modify the product data that is sent to Nosto.
     *
     * @return bool
     */
    public

    function install()
    {
        return (parent::install() && $this->registerHook('actionNostoProductLoadAfter'));
    }

    /**
     * Hook for modifying the Nosto product information before it is used the shop frontend or the server-to-server API.
     *
     * The input params will always include:
     *
     * - "nosto_product" which is the NostoTaggingProduct instance that has just been loaded with data
     * - "product" which is the Prestashop Product model instance used as source for the data
     * - "context" which is the Prestashop Context model instance used when loading the data
     *
     * Modifying the instance in $params['nosto_product'] will directly reflect on the data that is sent to Nosto. The
     * NostoTaggingProduct class has a public API of setter methods to modify it's content. Some common use cases are
     * document in below method.
     *
     * You can find a full reference of the available product fields in the Nosto support center.
     *
     * @param array $params
     *
     * @see https://support.nosto.com/get-started/tagging-product-pages/
     */
    public

    function hookActionNostoProductLoadAfter(array $params)
    {
        /** @var NostoTaggingProduct $nosto_product */
        $nosto_product = $params['nosto_product'];
        /*
        * Example 1
        *
        * Adding "tag1", "tag2" and "tag3" properties.
        *
        * These properties are used to include more information about the product that can in turn be used for more
        * granular control in the product recommendations.
        *
        * The tags are string values that can be added to the product.
        */

        // $nosto_product->addTag1('test_tag1');
        // $nosto_product->addTag2('test_tag2');
        // $nosto_product->addTag3('test_tag3');
        // This can be removed when implementing this module, it's here only to
        // check that the override works when implemented.

        $nosto_product->addTag3('nosto');
        /*
        * Example 2
        *
        * Replacing all the tags for "tag1", "tag2" or "tag3" properties.
        *
        * The "tag2" and "tag3" properties will be empty by default, so this method is an easy way to add a set of tag
        * values without looping through them. Be careful with the "tag1" property though, as that can contain pre-
        * defined values, which will be lost of using this method. For the "tag1" property it is safer to use the
        * method is `Example 1`.
        *
        * The format in an array containing string values.
        */

        // $nosto_product->setTag1(array('test1_tag1', 'test2_tag1'));
        // $nosto_product->setTag2(array('test1_tag2', 'test2_tag2'));
        // $nosto_product->setTag3(array('test1_tag3', 'test2_tag3'));

        /*
        * Example 3
        *
        * Changing the product image URL.
        *
        * If you are, for example, using a 3rd party cloud storage for your product images.
        *
        * The url must be absolute and includes the protocol (http or https).
        */

        // $nosto_product->setImageUrl('http://example.com/p/example.jpg');

    }
}

Once you've got the module into your PrestaShop installation, you should see it inside your PrestaShop backend module listing. Click the Install button next to the module to install it. Now you can start modifying the product information. The changes you make will be taken into use instantly both in the shop frontend (you can view the changes in the handy Nosto debug toolbar when navigating the product pages) and in the backend API requests.

How does it work? The Nosto module will dispatch an event called actionNostoProductLoadAfter whenever the product is loaded for frontend or backend tasks. By listening to this event you add/modify the product before it is used, and thus your changes will be included when the data is sent to Nosto.

Verifying

Once you have extended the product model and overridden whatever fields you may need, you should verify that it, in fact, working as expected.

A simple way to verify that the changes are working would be to view any product page in the Nosto debug-mode. The debug mode can be enabled by adding the query parameter nostodebug=true to the end of any product-page URL. This will cause a helpful debug toolbar to appear where you can view the product data on the page. For more information on the debug-toolbar, please refer to this guide titled Nosto Debug Toolbar in our Support Center.

If you were to extend the product model using the example given above, you would see that the "Tags" field in the debug-toolbar will read "nosto".

Please note that in order to verify the changes using the debug-toolbar, you must have a Nosto account for the given store.