1

I need to rating value for particular review which is given by add review.

Like if there are 5 customers given rating like 4,5,2,1.

I need that percentage value from Review Id.

Thanks in advance

2 Answers 2

0

You can use $reviewFactory via ObjectManager (not the best solution)

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$reviewFactory = $objectManager->create('Magento\Review\Model\Review');
$storeId = $this->_storeManager->getStore()->getId();
$reviewFactory->getEntitySummary($product, $storeId);
$ratingSummary = $product->getRatingSummary()->getRatingSummary();
1
  • Thank you, Madam. But this code display particular product average rating value. I need a particular review's rating value. Jul 3, 2019 at 11:20
0

Try the following code in your block class:

<?php
...
...

protected $ratingOption;
...
...
public function __construct(\Magento\Backend\App\Action\Context $context, \Magento\Review\Model\ResourceModel\Rating\Option\Vote\Collection $ratingOption) {
    parent::__construct($context);
    $this->ratingOption = $ratingOption;
}

Update 1:

Then call this code in your function. Let us say the function name is getRatingFromReviewId. So the function would look like this:

public function getRatingFromReviewId($reviewId) {
    $ratingCollection = $this->ratingOption
                             ->addRatingInfo()
                             ->addOptionInfo()
                             ->addRatingOptions()
                             ->addFieldToFilter('review_id', $reviewId);
    return $ratingCollection;
}

Now in your PHTML file, your code should be like:

$reviewId = 3;
$ratings = $block->getRatingFromReviewId($reviewId);
if(count($ratings)) {
    foreach($ratings as $rating) {
        ...
        YOUR CODE to calculate the AGGREGATE OF ALL RATINGS
        ...
    }
}

This will give you the percentage value of each rating criteria (i.e. price, quality, etc.)

You can use this info to get the aggregate value of all ratings.

I hope it makes sense to you.

4
  • yes its ok but i want to display in any phtml file so let me know sir. Jul 3, 2019 at 11:17
  • @sarveshDineshkumarPatel, I have updated my answer. Jul 3, 2019 at 11:26
  • @sarveshDineshkumarPatel, did my answer help you? Jul 5, 2019 at 9:16
  • actually I want not to display through the block. I want to display directly in .phtml file. Thank you, sir. Jul 5, 2019 at 9:33

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.