6

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

6

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

| improve this answer | |
4

Get start based data and all over average

\Magento\Review\Model\ResourceModel\Review\CollectionFactory $reviewCollectionFactory, \Magento\Review\Model\ReviewFactory $reviewFactory, \Magento\Review\Model\Review $review, \Magento\Catalog\Model\ProductFactory $product

$result = $items = $starData = [];      
$idArray = [2,5]; // product id
$reviewsCollection = $this->_reviewCollectionFactory->create()
    ->addFieldToFilter('entity_pk_value',array("in" => $idArray))
    ->addStatusFilter(\Magento\Review\Model\Review::STATUS_APPROVED)
    ->addRateVotes();
$reviewsCollection->getSelect();

foreach ($reviewsCollection->getItems() as $review) {

    foreach( $review->getRatingVotes() as $_vote ) {
        $rating = [];
        $percent = $_vote->getPercent();
        $star = ($percent/20);
        $productId = $_vote->getEntityPkValue();

        $productModel = $this->_product->create();
        $product = $productModel->load($productId);             
        $countReview = $this->_reviewFactory->create()->getTotalReviews($productId,false);
        $review_id = $_vote->getReviewId();

        $rating['review_id'] = $review_id;
        $rating['product_id'] = $productId;     
        $rating['percent']   = $percent;
        $rating['star']      = $star;
        $rating['nickname']  = $review->getNickname();                  
        $items[] = $rating;
        $starData[$star][] = $rating;
    }

}

if(count($items) > 0)   {           
    $result['all'] = $items;
    $result['star'] = $starData;            

    if(isset($starData[1]))
        $result ['count'][1] = count($starData[1]);
    else
        $result ['count'][1] = 0;

    if(isset($starData[2]))
        $result ['count'][2] = count($starData[2]);
    else
        $result ['count'][2] = 0;

    if(isset($starData[3]))
        $result ['count'][3] = count($starData[3]);
    else
        $result ['count'][3] = 0;

    if(isset($starData[4]))
        $result ['count'][4] = count($starData[4]);
    else
        $result ['count'][4] = 0;

    if(isset($starData[5]))
        $result ['count'][5] = count($starData[5]);
    else
        $result ['count'][5] = 0;

    $sum = $startSum = 0;

    foreach($result['count'] as $number => $count) {
        $sum += $number * $count;
        $startSum +=  $count;   
    }

    $avg =  $sum/$startSum;

    $result ['count']['all'] = count($result['all']);
    $result ['count']['avg'] = round($avg,1);
}

return $result; 
| improve this answer | |
  • 1
    +1 for ->addRateVotes() and $review->getRatingVotes() code. – Sukumar Gorai Sep 18 '19 at 12:43
  • +1 from my side too for ->addRateVotes() I didn't find this in any other answers. – Shoaib Munir Dec 3 '19 at 7:00
2
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$reviewFactory = $objectManager->create('Magento\Review\Model\Review');
$storeId = $storeManager->getStore()->getId();
$reviewFactory->getEntitySummary($product, $storeId);
$ratingSummary = $product->getRatingSummary()->getRatingSummary();
?>
<div class="rating-summary item" itemprop="reviewRating" itemscope="" >
    <div class="rating-result" title="<?php echo $ratingSummary; ?>%">
        <meta itemprop="worstRating" content="1">
        <meta itemprop="bestRating" content="100">
        <span style="width:<?php echo $ratingSummary; ?>%">
            <span itemprop="ratingValue"><?php echo $ratingSummary; ?>%</span>
        </span>
    </div>
</div>
| improve this answer | |
0

1 - I used like reference this file:

app/design/frontend/PATH/THEME/Magento_Catalog/templates/product/list.phtml

2 - Find this line:

<?php echo $block->getReviewsSummaryHtml($_product, $templateType); ?>

3 - Paste this lines after:

$review_model->getEntitySummary($_product);
$reviewRating = (int) $_product->getRatingSummary()->getRatingSummary();

// Show the value
var_dump($reviewRating);
| improve this answer | |
  • How to get the rating stars in my custom product scroller ? – Sushivam Mar 2 '17 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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