cancel
Showing results for 
Search instead for 
Did you mean: 

How to filter products by rating in Magento 2?

How to filter products by rating in Magento 2?

I want to get all products by rating filter. here my code

$getProducts = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$getProducts->addAttributeToSelect('*');
$getProducts->addAttributeToFilter('visibility', 4);

So, how can I add here filter for rating so I need not to first get all product and then check all product with that rating issue.

thanks

 

3 REPLIES 3

Re: How to filter products by rating in Magento 2?

Hello @sarvesh21007 

You can try the below code for filter product collection by rating percent:

$collection = $_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');$collection->addAttributeToSelect('*');$collection->getSelect()->joinLeft(array('rova'=> 'rating_option_vote_aggregated'),'e.entity_id =rova.entity_pk_value', array("percent" => 'percent'))->group('e.entity_id');$collection->getSelect()->where("rova.percent = 80");

foreach($collection as $product){
    //YOUR CODE
}

100 Percent = 5 Star Rating

90 Percent = 4.5 Star Rating

80 Percent = 4 Star Rating

 

or you use this extension

https://marketplace.magento.com/sunarc-magento2-ratingfilter.html

 

Was my answer helpful? You can accept it as a solution.
175+ Professional Extensions for M1 & M2
Need a developer?Just visit Contact Us Now

Re: How to filter products by rating in Magento 2?

@theMageComp Yes actually but I want average rating this return for all rating if there are like for price 60, value for 40 and quality for 20 and if I will filter by 20 then it also be in results but It should not be in results.

Re: How to filter products by rating in Magento 2?

Hello

In this post, I described all thing

you can use it.