Magento 2 – How to Get Rating Summary

magento2ratingsreview

I want the average rating of a product in Magento 2. How can I get the average or aggregate rating of product ?

Best Answer

You can try this

protected $_reviewFactory;

public function __construct(
    ...
    \Magento\Review\Model\ReviewFactory $reviewFactory,
    ...
) {
    ...
    $this->_reviewFactory = $reviewFactory;
    ...
}

public function getRatingSummary()
{
    ...
    $this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
    $ratingSummary = $product->getRatingSummary()->getRatingSummary();

    return $ratingSummary;
}

Or you can get $reviewFactory via ObjectManager (not the best solution)

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$reviewFactory = $objectManager->create('Magento\Review\Model\Review');

$storeId = $this->_storeManager->getStore()->getId();
$reviewFactory->getEntitySummary($product, $storeId);

$ratingSummary = $product->getRatingSummary()->getRatingSummary();

hope this helps