Question

Is there a build-in feature in Magento 2 by which top rated products can be shown? If not, then how can i display it in left side of category page. Any type of sample code would be helpful for me :)

Was it helpful?

Solution

public function getRatingSummary()
{

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $reviewFactory = $objectManager->get(\Magento\Review\Model\ReviewFactory::class);
    $storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
    $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    $collection = $productCollection->create()
        ->addAttributeToSelect('*')
        ->load();
    $rating = array();
    foreach ($collection as $product) {
        $reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
        $ratingSummary = $product->getRatingSummary()->getRatingSummary();
        if($ratingSummary!=null){
            $rating[$product->getId()] = $ratingSummary;
        }
    }
    return $rating;
}  
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top