Skip to content

Overriding Product Data

Hannu Pölönen edited this page May 16, 2018 · 14 revisions

In order to modify the product data that is sent to Nosto, you need to create your own mini-module. Please read the basics how to extend or override data in Nosto extension

For modifying Nosto's product data you must register hook actionNostoProductLoadAfter and implement corresponding action method hookActionNostoProductLoadAfter(). Below is an example how your MyNosto class could look like.

File modules/mynosto/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'));
    }

    /**
     * Action method 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 NostoProduct $nosto_product */
        $nosto_product = $params['nosto_product'];

        /** @var NostoTaggingProduct $prestashop_product_product */
        $prestashop_product = $params['product'];

        if ($nosto_product instanceof NostoProduct) {
           // Implement your customizations here
          $nosto_product->addTag3('nosto test');
          $nosto_product->setPrice($nosto_product->getPrice()*1.2);
          $nosto_product->setName($nosto_product->getName() . ' - TEST OVERRIDE');
        }
    }
}