In order to modify the product data that is sent to Nosto you must create a small Magento 2 module. Start by following the steps described in the wiki page Extending Nosto's Module.
By creating a M2 module you can override the product model in a way that the modifications take place in API calls, exports and in the tagging.
Required files and configurations
1. The events config app/code/My/Nosto/etc/events.xml
2. The observer app/code/My/Nosto/Observer/Product/Load.php
<?phpnamespaceMy\Nosto\Observer\Product;useMagento\Framework\Event\Observer;useMagento\Framework\Event\ObserverInterface;classLoadimplementsObserverInterface{publicfunction__construct() { }/** * Observer for "nosto_product_load_after" event * * @paramObserver $observer * @returnvoid */publicfunctionexecute(Observer $observer) {/* @var $product \NostoProduct */ $product = $observer->getProduct();/* * Here you can modify any product property you need and it will * automatically be used both in the store front tagging and the * server-to-server API calls. * * Note that any new properties will not automatically be used. * * @see https://support.nosto.com/get-started/tagging-product-pages/ *//* * Example 1 * * Adding "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. * * It is important to notice that the `addTag()` function receives a string as an argument * and can be called multiple times in order to add multiple arguments. * If you call: * $product->addTag3('example1'); * $product->addTag3('example2'); * You will have `example1` and `example2` as in the tag3 field. * * The `setTag` function takes an array of strings as argument and * will override any previously assigned values to the tag: * * $product->setTag2(['example1', 'example2']); * $product->setTag3(['example3']); * *//* * Example 2 * * 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). * $product->setImageUrl('http://example.com/p/example.jpg'); * */// This can be removed when implementing this extension, it's here only to// check that the override works when implemented. $product->addTag3('nosto'); }}
Adding custom attributes to product tags
<?phpnamespaceMy\Nosto\Observer\Product;useMagento\Framework\Event\Observer;useMagento\Framework\Event\ObserverInterface;useMagento\Catalog\Api\ProductRepositoryInterface;classLoadimplementsObserverInterface{protected $productRepository;publicfunction__construct(ProductRepositoryInterface $productRepository) {$this->productRepository = $productRepository; }/** * Observer for "nosto_product_load_after" event * * @paramObserver $observer * @returnvoid * @throws\Magento\Framework\Exception\NoSuchEntityException */publicfunctionexecute(Observer $observer) {/* @var $product \NostoProduct */ $product = $observer->getProduct();// Get the Magento product $magentoProduct =$this->productRepository->getById($product->getProductId());// If you have a custom attribute (string value) stored in the Magento// product model, it can be added to a tag field like this: $myAttribute = $magentoProduct->getCustomAttribute('my_custom_attribute');if($myAttribute) { $product->addTag3($myAttribute->getValue()); } }}
If all the steps were followed correctly, your final directory structure should have these files:
Run the following commands to update the generated code in Magento's end
bin/magento module:enable --clear-static-content My_Nosto
bin/magento setup:upgrade
As of module version >= 4.0.0 Nosto product data is cached and thus you must run full reindex for Nosto indexers in order for the changes to be reflected to Nosto.