1

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

  • First, using objectManager is not good practice. And do you want only products of home-page ? – Jai Jun 3 '17 at 11:43
  • not exactly of home-page products, i have to put slider over there, if it is static collection then also it would be fine – Zeolr Jun 3 '17 at 11:45
  • ok.that means you want to put all 5-start-reviewed products on home-page. – Jai Jun 3 '17 at 11:47
  • very true, kind of widget – Zeolr Jun 3 '17 at 11:56
0

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/>';
}
| improve this answer | |
  • this works for me @Sohel, rating_summary 100 like it – Zeolr Jun 3 '17 at 16:18

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.