14

I want to display ratings on the product page like this:

enter image description here

  1. How many users selected 5/4/3/2/1 star ratings
  2. The average rating, displayed as stars
  3. Percentage of users selected a 5 star rating

I need to add custom code for that, so please don't suggest getReviewsSummaryHtml.

  • Can you please post you complete solution here? I need to do same but not good in doing it. – Jai Feb 9 '17 at 11:33
8

Create and call this new method with product ID. Also make sure you have only 1 rating option enable in admin.

public function getAllStart($pid) {
    $review = $this->_objectReview->getCollection()     //\Magento\Review\Model\Review $reviewFactory (_objectReview)
            ->addFieldToFilter('main_table.status_id', 1)
            ->addEntityFilter('product', $pid)          //$pid = > your current product ID
            ->addStoreFilter($this->_storeManager->getStore()->getId())
            ->addFieldToSelect('review_id')
    ;
    $review->getSelect()->columns('detail.detail_id')->joinInner(
            ['vote' => $review->getTable('rating_option_vote')], 'main_table.review_id = vote.review_id', array('review_value' => 'vote.value')
    );
    $review->getSelect()->order('review_value DESC');
    $review->getSelect()->columns('count(vote.vote_id) as total_vote')->group('review_value');
    for ($i = 5; $i >= 1; $i--) {
        $arrRatings[$i]['value'] = 0;
    }
    foreach ($review as $_result) {
        $arrRatings[$_result['review_value']]['value'] = $_result['total_vote'];
    }
    return $arrRatings;
    }
  • yes, I have only single rating active from admin. so let me try this code – Ronak Chauhan Jan 31 '17 at 6:27
  • Thanks a lot :) It's working fine. Many many thanks again – Ronak Chauhan Jan 31 '17 at 6:45
  • Where to add this code to above result? I need to do same thing. – Jai Feb 9 '17 at 10:33
  • @Jai, You have to put this code in any of your custom module block file and call this method from where you want. Like you can call it from review list page of your theme. – Ashish Jagnani Feb 9 '17 at 10:43
  • Can you Please do so I can follow accordingly? You can add how you had done in your work. Post the code of block you created and how you call in review list page. – Jai Feb 9 '17 at 11:16

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.