2

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 :)

1 Answer 1

2
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;
}  
1
  • Can you please post template file too? Commented Sep 3, 2020 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.