0

How to get ratings in a group count of 5 star, 4 star, 3 star, 2 star and 1 star in Magento 2.0 in form.phtml of Magento_Review template.enter image description here

6
  • I don't think that you can do it with 3 attributes for rating. You will need to use only 1 rating criteria first (Right now in default magento there are price, quality and value, so total rating is average of 3 criteria, which can be decimal). Aug 9, 2016 at 7:35
  • Yes I am using only 1 for rating. But how we call Rating Summery in Magento 2 on rating form page. Aug 9, 2016 at 9:49
  • Have you got solution for this Question? Jan 31, 2017 at 5:49
  • @RonakChauhan, Yes I create custom function and query for this. Jan 31, 2017 at 6:24
  • can you give me that code? Jan 31, 2017 at 6:26

2 Answers 2

1

You have to create and call this method with your product ID. Also make sure you have only 1 rating option 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;
    }
2
  • it's not working if the site have multiple criterias ratings.
    – huykon225
    Dec 11, 2020 at 8:00
  • admin rating change after display Error frontend "Item (Magento\Review\Model\Review) with the same ID "364" already exists."
    – Himanshu
    Feb 11, 2021 at 9:32
0

I think what you are asking is how to get a rating summary of all of the ratings in 1.

If that is the case you simply need to call the ratings summary template.

Check out this question:

How to get rating summary in Magento 2?

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.