Open navigation

Synchronising Third-Party Product Rating and Review Count Data

The Klevu extension only supports native Magento product reviews. If you are using a third-party or custom reviews provider or integration then that data will not be reflected in your search results without custom development.

For more information about how native reviews and rating data are collected and synchronized, please see this article.

These instructions apply to Klevu version 2.9.0 / 3.1.0 and higher, which introduced a number of new internal services.

You will need to create a new custom module to extend the default Klevu behavior. Ensure this module depends on Klevu_Search in your etc/module.xml file.

Once you have your extension, you will need to add a new data provider to retrieve and format rating and review count information. This class should implement \Klevu\Search\Api\Provider\Catalog\Product\Review\AllRatingsDataProviderInterface, which has a single getData method receiving a store id for which to return data.

The specifics of retrieving the data are outside the scope of this guide,  but a simple example using a hardcoded set of data is below

<?php

declare(strict_types=1);

namespace Acme\KlevuRatings\Provider;

use Klevu\Search\Api\Provider\Catalog\Product\Review\AllRatingsDataProviderInterface;
use Klevu\Search\Service\Catalog\Product\Review\RatingDataMapper;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\Store;

class AllRatingsDataProvider implements AllRatingsDataProviderInterface
{
    /**
     * @param StoreInterface|int $store
     * @return array
     */
    public function getData($store)
    {
        // Get reviews information from desired service
        // This example is for illustrative purposes only

        $sourceData = [
            [
                'product' => 1,
                'average_rating' => 3.14,
                'reviews_with_ratings' => 42,
                'total_reviews' => 123,
            ],
        ];

        $return = [];

        $storeId = ($store instanceof StoreInterface) ? (int)$store->getId() : (int)$store;
        if ($storeId === Store::DEFAULT_STORE_ID) {
            /** Data should never be saved in the default scope */
            return $return;
        }

        foreach ($sourceData as $row) {
            $return[] = [
                RatingDataMapper::RATING_AVERAGE => $row['average_rating'] * 20, 
                RatingDataMapper::RATING_COUNT => $row['reviews_with_ratings'],
                RatingDataMapper::RATING_PRODUCT_ID => $row['product'],
                RatingDataMapper::RATING_STORE => $storeId,
                RatingDataMapper::RATING_SUM => ($row['average_rating'] * 20) * $row['reviews_with_ratings'],
                RatingDataMapper::REVIEW_COUNT => $row['total_reviews'],
            ];
        }

        return $return;
    }
}

You can then inject this into the Klevu update service in etc/di.xml, replacing the existing provider

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!-- Replace ratings data provider -->
<type name="Klevu\Search\Service\Catalog\Product\Review\UpdateAllRatings">
<arguments>
<argument name="allRatingsDataProvider" xsi:type="object">Acme\KlevuRatings\Provider\AllRatingsDataProvider</argument>
</arguments>
</type>

<!-- snip -->
</config>

By this stage, any time the ratings are regenerated (via events or manually through the CLI), the customs data will be stored against the product rather than data from Magento’s native reviews.

To prevent regeneration running for native review related actions, you should disable the Klevu plugins and observers like so

etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> 
    <!-- snip -->
    
    <!-- Disable Klevu ratings plugins -->
    <type name="Magento\Review\Model\ResourceModel\Rating\Option">
        <plugin name="Klevu_Search::updateRatingAttributesOnAggregate" disabled="true"/>
    </type>
</config>

etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <!-- Disable Klevu ratings event observers -->
    <event name="review_save_after">
        <observer name="ratingsUpdate" disabled="true"/>
    </event>
    <event name="review_delete_after">
        <observer name="ratingsDelete" disabled="true"/>
    </event>
</config>

You will then need to implement your own observers/plugins to trigger the regeneration of rating attributes accordingly. The specifics of this change depend on the service being integrated and is outside the scope of this guide.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.