Mastodon

I’ve spent the past few days creating a more up to date version of my Category Featured Product module for Magento.

NOTE: If you just want the solution, skip to the end of this post where I’ve written a quick summary

One of the new features that it has is the ability to display the product review summary, just like you’d get on a category page.

At first I thought it would be quite simple, simply load the product attributes and then call the usual code in the template as follows:

<?php if($_product->getRatingSummary()): ?>
 <?php echo $this->getReviewsSummaryHtml($_product) ?>
 <?php endif; ?>

Unfortunately this piece of code returns nothing and no review summary is displayed.  After a quick var_dump of $_product->getRatingSummary() I noticed that the value being returned was null

Owing to this, I set about trying to find a solution by looking at how the Product List Block works and seeing if I could find anything related in there. Unfortunately there were no clues in there, although I did pick up some other nifty little tricks (will share another day).

After 2 hours searching, I finally managed to find how it’s done…
When the Product List block is about to be rendered, the _beforeToHtml() is run, this in turn dispatches an event:

Mage::dispatchEvent('catalog_block_product_list_collection', array(
 'collection' => $this->_getProductCollection()
 ));

A quick search for catalog_block_product_list_collection in my IDE led me to:
app/code/core/Mage/Review/etc/config.xml
I knew I was about to find the solution as soon as I saw “Review” in the path!

I opened up the file, and found this:

<catalog_block_product_list_collection>
    <observers>
        <review>
            <type>model</type>
            <class>review/observer</class>
           <method>catalogBlockProductCollectionBeforeToHtml</method>
       </review>
    </observers>
</catalog_block_product_list_collection>

I quickly opened up app/code/core/Mage/Review/Model/Observer within which I found the catalogBlockProductCollectionBeforeToHtml method (as mentioned in the XML above).

This method contained the following code:

$productCollection = $observer->getEvent()->getCollection();
if ($productCollection instanceof Varien_Data_Collection) {
    $productCollection->load();
    Mage::getModel('review/review')->appendSummary($productCollection);
}

And obviously this was the solution 😀
I went back to my code and added in the following lines:

$collection->load();
Mage::getModel('review/review')->appendSummary($collection);
return $collection;

After refreshing the page, I was seeing all the product reviews and it was a big win all round. It’s been quite a journey finding the solution, but as is usually the case with such mighty investigations, the sense of elation upon discovery can only be compared to that experienced when finding money under the sofa cushions.

Summary

For those who don’t want to read the entire post (I’m not one to judge and call lazy) here’s a quick summary of the solution:

In your template put (use whatever variable name you assign to your product):

<?php if($_product->getRatingSummary()): ?>
 <?php echo $this->getReviewsSummaryHtml($_product) ?>
 <?php endif; ?>

After filtering your collection, just about it’s going to be passed to the view run the following:

$collection->load();
Mage::getModel('review/review')->appendSummary($collection);
return $collection;

Hope this helps!

3
0
Would love your thoughts, please comment.x
()
x