Customised Magento review summary
06 Jul 2009, by Denis Margetic
4 Comments · Posted in Magento

    There is little doubt that product reviews are popular with online shoppers, and are playing an increasingly important role in purchasers' buying decisions. Magento includes a solid feature set for implementing product reviews and can show them on various pages that include product listings. You may find that you want to customise the way these reviews are displayed, but the best way to do this may not be immediately obvious.

    The standard review summary text for products without reviews is:

    In cases with one or more existing reviews it shows the rating followed by some links, as seen in this example:

    And if you inspect the relevant phtml files you will find this code, which is responsible for inserting the review summary html:

    getReviewsSummaryHtml($_product, false, true)?>

    But what if you needed to make changes to this? If you want to change the look for all instances of the review summary you can simple change the review summary phtml file, namely:

    /template/review/helper/summary.phtml

    If, on the other hand you would like to have a different looking review summary in some cases, whilst leaving the default look for others, you can indeed do this and it is very easy. Magento actually provides a shorter version of the review summary, but this is not obvious from the existing code. For some reason the second argument in the getReviewsSummaryHtml function call is incorrectly set to 'false' when it really isn't a boolean, but is actually a keyword for a review summary template. Magento comes with a shorter version of the review summary which you can access by simply replacing the false in the second argument with "short", like this:

    getReviewsSummaryHtml($_product, "short", true)?>

    Now if there is a review for a product it will be appear like this

    and if you want to edit this template, you can find it here

    /template/review/helper/summary_short.phtml

    You can go a step further and add your custom templates as well. However, to do this you will need to edit one Magento core file and you will need to add your own phtml template.

    First, add your own phtml file in the template/review/helper/directory. Probably the best thing to do here is to make a copy of the existing summary.phtml file and modify it to fit your needs. Let's say you called your new file myreview.phtml.

    You need to add a reference to this template (a keyword and the path to your phtml template) in the following core file:

    /app/code/core/Mage/Review/Block/Helper.php

    in the $_availableTemplates array. Once you have added the necessary line, the top of the file should look like this:

    class Mage_Review_Block_Helper extends Mage_Core_Block_Template
    {
        private $_availableTemplates = array(
            'default' => 'review/helper/summary.phtml',
    	'short'   => 'review/helper/summary_short.phtml'
    	'myreview'=> 'review/helper/myreview.phtml'
        );
    
    	public function getSummaryHtml($product, $templateType, $displayIfNoReviews)
    	{
    ...
    

    Note the usual warning that you should not edit the actual Magento core file, but should first copy it to your local directory. The file to edit is:

    /app/code/local/Mage/Review/Block/Helper.php

    Your template is ready to go. To use it in your html code simply insert the following line:

    getReviewsSummaryHtml($_product, "myreview", true)?>

    Comments

    Great article thanks.
    What about removing the review summary off any product listings, so reviews only appear on the product page it self?
    Comment by Anonymous - 3 Apr 2010 8:22:22 PM
    How to put html code in magento reviews?
    Comment by See World - 23 Aug 2010 11:39:13 AM
    Excellent! thanks guys! companies like yours are definitely the future of open source!
    Comment by Anonymous - 23 Sep 2010 8:23:44 AM
    You don't need to edit core files to add a custom template.
    Just call the corresponding Helper Method "addTemplate" in Mage_Review_Block_Helper in your catalog.xml.

    Let's say the Block you want to call the custom template from is: [code]<block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">[/code], you add into this block the following line:

    [code]<action method="addReviewSummaryTemplate"><type>myType</type><template>review/helper/myTypeTemplate.phtml</template></action>[/code]

    Then, make a copy of the existing review Helper Templates and rename it to "myTypeTemplate.phtml":

    [code]/template/review/helper/myTypeTemplate.phtml[/code]

    Then you can safely edit yout custom Template and you're done.
    Comment by Mo - 11 Jan 2011 7:09:00 PM
    Comments are closed for this post