I am trying to get product reviews filtered by Product Id, but in the product which doesn't have any reviews added, the reviews count still shows 2 reviews.

This means review collection filter by Product Id isn't working.

Below is the code I am using to get Review collection.

$_product = $this->getProduct();
$productId = $_product->getId();
$reviews = Mage::getModel('review/review')->getResourceCollection()
        ->addEntityFilter('product', $productId)
$avg = 0;
$ratings = array();
if (count($reviews) > 0){
foreach ($reviews as $_review): ?>
<?php foreach( $_review->getRatingVotes() as $_vote ): ?>
<?php $ratings[] = $_vote->getPercent(); ?>
<?php endforeach; ?>
<?php endforeach;
$avg = array_sum($ratings)/count($ratings); }

Can somebody let me know what might be going wrong with this ?

3 Answers 3


There's nothing wrong with the posted code. I've checked it against with sample data and using productId 337. I get a count of 6 each time and have been trying to break it, unsuccessfully (test script is below).

That means the error is in the code that you're not showing: the actual count() in the template file.

Test code used:

if( extension_loaded('xdebug') )
    ini_set('xdebug.cli_color', true);
    ini_set('xdebug.var_display_max_data', -1); // unlimited
$productId = 337;
$reviews = Mage::getModel('review/review')->getCollection()
    ->addEntityFilter('product', $productId)

echo(str_repeat('-', 80).PHP_EOL);
/** @var Mage_Review_Model_Review $review */
foreach($reviews AS $review)
    /** @var Mage_Rating_Model_Resource_Rating_Option_Vote_Collection $votes */
    $votes = $review->getRatingVotes();
    $total = 0;
    foreach($votes AS $vote)
        $total += $vote->getPercent();
    $avg = $total / count($votes);
    var_dump($total, count($votes), $avg);

$reviews = Mage::getModel('review/review')->getCollection() ->addStoreFilter(Mage::app()->getStore()->getId()) ->addFieldToFilter('entity_id', $productId) ->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED) ->setDateOrder() ->addRateVotes();

Please use this collection to get review by product id.


Maybe it's late, but you have to use: $reviews->length to check the retrieved values count, count() return the number of key of the $review object

  • No, collections implement the Countable interface, so count($reviews) is the same as $reviews->count() which returns the number of loaded items. Dec 8, 2015 at 10:20

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.