Skip to content

Magento2 | Get ratings for each review

I am trying to take product reviews programmatically, filter them by product and pull out the rating stars.

I can get the text of the reviews (getDetail()) without any problems and in fact I see all the review texts including the creation dates. What I can’t see is the rating score of the review. Whenever I try to print out getRatingVotes the value always returns 0. As if the ratings are not saved but they are actually there. This is my code:

<?php
    $product_id = $_product->getId();
    $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    $rating = $objectManager->create('MagentoReviewModelRating');
    $review = $objectManager->create('MagentoReviewModelReview');
    $storeManager = $objectManager->create("MagentoStoreModelStoreManagerInterface");
    $_ratingSummary = $rating->getEntitySummary($product_id);
    $ratingCollection = $review->getResourceCollection()
                        ->addStatusFilter(MagentoReviewModelReview::STATUS_APPROVED)
                        ->addEntityFilter('product', $product_id)
                        ->setDateOrder()
                        ->addRateVotes();
    $review_count = count($ratingCollection); // How many review in that specific product
    
    
    
    
    
    foreach($ratingCollection->getItems() as $ratingItem){
            echo $ratingItem->getDetail().' '.'<br/>';
            if(count($ratingItem->getRatingVotes()) > 0){
                foreach($ratingItem->getRatingVotes() as $_vote){
                    echo $_vote->getPercent().'<br/>';
                }
            }
    }
?>

How come I can’t see the ratings score for the review?