0

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 = \Magento\Framework\App\ObjectManager::getInstance();
    $rating = $objectManager->create('\Magento\Review\Model\Rating');
    $review = $objectManager->create('\Magento\Review\Model\Review');
    $storeManager = $objectManager->create("\Magento\Store\Model\StoreManagerInterface");
    $_ratingSummary = $rating->getEntitySummary($product_id);
    $ratingCollection = $review->getResourceCollection()
                        ->addStatusFilter(\Magento\Review\Model\Review::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?

1 Answer 1

0
\Magento\Review\Model\ResourceModel\Review\CollectionFactory $reviewCollectionFactory,
    \Magento\Review\Model\ReviewFactory $reviewFactory,
    \Magento\Review\Model\Review $review,
    \Magento\Catalog\Model\ProductFactory $product

    
    $result = $items = $starData = [];      
    $idArray = [2,5]; // product id
    $reviewsCollection = $this->_reviewCollectionFactory->create()
        ->addFieldToFilter('entity_pk_value',array("in" => $idArray))
        ->addStatusFilter(\Magento\Review\Model\Review::STATUS_APPROVED)
        ->addRateVotes();
    $reviewsCollection->getSelect();

    foreach ($reviewsCollection->getItems() as $review) {

        foreach( $review->getRatingVotes() as $_vote ) {
            $rating = [];
            $percent = $_vote->getPercent();
            $star = ($percent/20);
            $productId = $_vote->getEntityPkValue();
            
            $productModel = $this->_product->create();
            $product = $productModel->load($productId);             
            $countReview = $this->_reviewFactory->create()->getTotalReviews($productId,false);
            $review_id = $_vote->getReviewId();
            
            $rating['review_id'] = $review_id;
            $rating['product_id'] = $productId;     
            $rating['percent']   = $percent;
            $rating['star']      = $star;
            $rating['nickname']  = $review->getNickname();                  
            $items[] = $rating;
            $starData[$star][] = $rating;
        }
        
    }
    
    if(count($items) > 0)   {           
        $result['all'] = $items;
        $result['star'] = $starData;            
        
        if(isset($starData[1]))
            $result ['count'][1] = count($starData[1]);
        else
            $result ['count'][1] = 0;
            
        if(isset($starData[2]))
            $result ['count'][2] = count($starData[2]);
        else
            $result ['count'][2] = 0;
            
        if(isset($starData[3]))
            $result ['count'][3] = count($starData[3]);
        else
            $result ['count'][3] = 0;
            
        if(isset($starData[4]))
            $result ['count'][4] = count($starData[4]);
        else
            $result ['count'][4] = 0;
        
        if(isset($starData[5]))
            $result ['count'][5] = count($starData[5]);
        else
            $result ['count'][5] = 0;
        
        $sum = $startSum = 0;
        
        foreach($result['count'] as $number => $count) {
            $sum += $number * $count;
            $startSum +=  $count;   
        }
        
        $avg =  $sum/$startSum;
        
        $result ['count']['all'] = count($result['all']);
        $result ['count']['avg'] = round($avg,1);
    }
    
    return $result; 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.