I want the average rating of a product in Magento 2. How can I get the average or aggregate rating of product ?
4 Answers
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
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;
-
Thanks a lot for pointing out
->addRateVotes()
. You just saved the day! Commented May 3, 2023 at 14:01
<?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>
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);
-
How to get the rating stars in my custom product scroller ?– SushivamCommented Mar 2, 2017 at 7:38