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
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI 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
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();
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.