Question

Magento how to get rating star by review id

enter image description here

I used the following code

$ratingCollection = Mage::getModel('rating/rating_option_vote')
     ->getResourceCollection()
     ->setReviewFilter($_review->getId())
     ->setStoreFilter(Mage::app()->getStore()->getId())
     ->addRatingInfo(Mage::app()->getStore()->getId())
     ->load()
     ->getItems();

     $ratings = array();
     foreach($ratingCollection as $item){
        $ratings[]=$item->getValue();
    }

but I am getting array summary as 12, 15 but I need %

Thanks

Was it helpful?

Solution

The rating_option_vote table saves to vote option for a particular rating type.

This table has review_id as the foreign key which references to review table.

Please check screenshots for reference.

enter image description here

enter image description here

Update: Code for getting ratings per review:

<?php
$reviewcollection = Mage::getModel('review/review')->getCollection()
                ->addStoreFilter(Mage::app()->getStore()->getId())
                ->setDateOrder()->addRateVotes();
        $_items = $reviewcollection;
// echo "<pre>";print_r($_items->getData());echo "</pre>";
        foreach ($_items as $_review) {
            $_votes = $_review->getRatingVotes();
            if (count($_votes)) {
                foreach ($_votes as $_vote) {
                    echo $this->escapeHtml($_vote->getRatingCode());
                    echo "<br/>";
                    echo $_vote->getPercent();
                }
            }
        }
?>

Update: You can also try below code:

<?php
$votesCollection = Mage::getModel('rating/rating_option_vote')
    ->getResourceCollection()
    ->setReviewFilter(1)
    ->setStoreFilter(Mage::app()->getStore()->getId())
    ->load();
// echo "<pre>";print_r($votesCollection);echo "</pre>";
    foreach ($votesCollection as $vote) {
        # code...
        echo "<pre>";print_r($vote);echo "</pre>";
    }
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top