Fetch products with 5 star reviews
-
09-10-2020 - |
Question
I'm trying to fetch all the products which are having 5 star rating on home page.
Used objectManager for product collection.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$reviewFactory = $objectManager->create('Magento\Review\Model\Review');
$storeId = $this->_storeManager->getStore()->getId();
foreach ($collection as $product) {
$reviewFactory->getEntitySummary($product, $storeId);
$ratingSummary = $product->getRatingSummary()->getRatingSummary();
}
Not finding any solution in Magento 2.
Thanks you
Solution
Try following way:
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->productCollection = $productCollection;
$this->storeManager = $storeManager;
}
And then:
$productCollection = $this->productCollection->addAttributeToSelect("*")->joinField('rating_score',
'review_entity_summary',
'rating_summary',
'entity_pk_value=entity_id',
array('entity_type'=>1, 'store_id'=> $this->storeManager->getStore()->getId(), 'rating_summary'=> 100),
'right'
);
foreach ($productCollection as $product) {
echo $product->getSku().' '.$product->getName().'<br/>';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange