2

I want to add sorting in Review Listing. What is the logic to add Sort by ratings?

enter image description here

I have override method getReviewsCollection() of Magento/Review/Block/Product/View.php So I have $this->_reviewsCollection on my hand.

Anyone has Idea how to apply sort on base of rating?

1 Answer 1

1

I found the Solution as below:

$countColumn = new \Zend_Db_Expr("COUNT(*)");
$percentColumn = new \Zend_Db_Expr("SUM(percent)/".$countColumn );

$this->_reviewsCollection->getSelect()->joinLeft(
   ['rating_vote'=>$this->_reviewsCollection->getTable('rating_option_vote')],
   'rating_vote.review_id=main_table.review_id',
   ['sum' => 'SUM(percent)', 'count' => $countColumn, 'average' => $percentColumn]);

$this->_reviewsCollection->getSelect()->group('rating_vote.review_id');

$this->_reviewsCollection->getSelect()->order('average desc');
1
  • bro i have a similar problem but for backend, can you take a look? Jul 20, 2022 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.