In this post I am going to explain how to get product reviews with rating in magento 2.
1. Using Dependency Injection
Add below code snippet in Block class.
protected $_reviewCollectionFactory;
protected $_storeManager;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Review\Model\ResourceModel\Review\CollectionFactory $reviewCollectionFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
array $data = []
)
{
$this->_reviewCollectionFactory = $reviewCollectionFactory;
$this->_storeManager = $storeManager;
parent::__construct($context, $data);
}
public function getCurrentStoreId() {
return $this->_storeManager->getStore()->getId();
}
public function getReviewsCollection()
{
$currentStoreId = $this->getCurrentStoreId();
$reviewsCollection = $this->_reviewCollectionFactory->create()
->addFieldToSelect('*')
->addStoreFilter($currentStoreId)
->addStatusFilter(\Magento\Review\Model\Review::STATUS_APPROVED)
->setDateOrder()
->addRateVotes();
return $reviewsCollection;
}Add below code snippet in template file.
// Get reviews collection
$reviewsCollection = $block->getReviewsCollection();
echo "<pre>";
print_r($reviewsCollection->getData());
echo "</pre>";
$dateFormat = $block->getDateFormat() ? : \IntlDateFormatter::SHORT;
if ($reviewsCollection && count($reviewsCollection) > 0) {
foreach ($reviewsCollection AS $review) {
echo $review->getTitle() . "<br/>";
echo $review->getDetail() . "<br/>";
echo $review->getNickname() . "<br/>";
echo $block->formatDate($review->getCreatedAt(), $dateFormat) . "<br/>";
// Display Average Rating of Review
$countRatings = count($review->getRatingVotes());
if ($countRatings > 0) {
?>
<div class="review-ratings">
<?php
$allRatings = 0;
foreach ($review->getRatingVotes() as $vote) {
$allRatings = $allRatings + $vote->getPercent();
}
$allRatingsAvg = $allRatings / $countRatings;
?>
<div class="rating-summary">
<div class="rating-result" title="<?php echo $allRatingsAvg; ?>%">
<meta itemprop="worstRating" content = "1"/>
<meta itemprop="bestRating" content = "100"/>
<span style="width:<?php echo $allRatingsAvg; ?>%">
<span itemprop="ratingValue"><?php echo $allRatingsAvg; ?>%</span>
</span>
</div>
</div>
</div>
<?php
}
}
}
2. Using Object Manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
//Get current store id
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$currentStoreId = $storeManager->getStore()->getId();
$reviewCollectionFactory = $objectManager->create('Magento\Review\Model\ResourceModel\Review\CollectionFactory')->create();
// Get reviews collection
$reviewsCollection = $reviewCollectionFactory->addFieldToSelect('*')
->addStoreFilter($currentStoreId)
->addStatusFilter(\Magento\Review\Model\Review::STATUS_APPROVED)
->setDateOrder()
->addRateVotes();
echo "<pre>";
print_r($reviewsCollection->getData());
echo "</pre>";
$dateFormat = $block->getDateFormat() ? : \IntlDateFormatter::SHORT;
if ($reviewsCollection && count($reviewsCollection) > 0) {
foreach ($reviewsCollection AS $review) {
echo $review->getTitle() . "<br/>";
echo $review->getDetail() . "<br/>";
echo $review->getNickname() . "<br/>";
echo $block->formatDate($review->getCreatedAt(), $dateFormat) . "<br/>";
// Display Average Rating of Review
$countRatings = count($review->getRatingVotes());
if ($countRatings > 0) {
?>
<div class="review-ratings">
<?php
$allRatings = 0;
foreach ($review->getRatingVotes() as $vote) {
$allRatings = $allRatings + $vote->getPercent();
}
$allRatingsAvg = $allRatings / $countRatings;
?>
<div class="rating-summary">
<div class="rating-result" title="<?php echo $allRatingsAvg; ?>%">
<meta itemprop="worstRating" content = "1"/>
<meta itemprop="bestRating" content = "100"/>
<span style="width:<?php echo $allRatingsAvg; ?>%">
<span itemprop="ratingValue"><?php echo $allRatingsAvg; ?>%</span>
</span>
</div>
</div>
</div>
<?php
}
}
}Thats it. Enjoy Magento 2!!


September 1, 2020 at 10:38 am
How To get the product and link in this review collection