cancel
Showing results for 
Search instead for 
Did you mean: 

How to get rating summary in Magento 2?

SOLVED

How to get rating summary in Magento 2?

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

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to get rating summary in Magento 2?

I got the solution here

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 others as well

 

 

View solution in original post

3 REPLIES 3

Re: How to get rating summary in Magento 2?

I got the solution here

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 others as well

 

 

Re: How to get rating summary in Magento 2?

I am getting following error
main.CRITICAL: Notice: Undefined variable: product

Re: How to get rating summary in Magento 2?

Hello Suyash,

You need to pass project object. The product which review you want for the reference.