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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
// Get reviews collection $reviewsCollection = $block->getReviewsCollection(); echo "<pre>"; print_r($reviewsCollection->getData()); echo "</pre>"; $dateFormat = $block->getDateFormat() ? : \IntlDateFormatter::SHORT; if ($reviewsCollection && count($reviewsCollection) > ) { 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 > ) { ?> <div class="review-ratings"> <?php $allRatings = ; 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
$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) > ) { 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 > ) { ?> <div class="review-ratings"> <?php $allRatings = ; 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