Search code examples
urlmagentosidebar

Getting the reviews page URL for a product in the product page sidebar


I am trying to get the url for a products reviews page in the sidebar on that products page. I know this can't be that difficult, but it is defeating me at the moment..

I can get the product page URL (basically the url for the page the sidebar is on) but not the reviews page... which is essentially the same url with -reviews.htm at the end instead of just .htm

Where am I going wrong? What call do I have to make?


Solution

  • In your (your theme) catalog.xml file find the section beginning with <catalog_product_view translate="label">

    Look for <reference name="right">.

    If your template does not have a right section in product view, add one in below content and enter:

        <reference name="right">
        <block type="review/product_view_list" name="product.info.product_additional_data" as="reviews" template="review/product/view/list.phtml"/>   
        </reference>
    

    Make sure you have cache off, load your product page (hopefully a product with a review on there) and you should now have the necessary.

    You'll also be wanting to have the 'add a review' box on the product page to make it easier for people to add a review. There are some really clumsy ways to do this that don't work properly. However, this is the easy, simple way...

    Open the same layout.xml file you had earlier, go to the catalog_product_view section, go right to the bottom of the content section. Look for the final closing </reference> tag. Now add:

    <block type="review/form" name="product.review.form" as="review_form" template="review/form.phtml"/>
    

    Now go to the front end, add your testimonial and note how wonderfully it all works, complete with theme etc.

    Hopefully, with this example you will begin to understand how powerful and useful the Magento layout xml files are.

    Extra

    Since it is a new block that is needed, you need new template file.

    Add:

    app/design/frontend/base/default/template/review/sidebar.phtml

    Enter into it something like:

     <div class="block block-reviews">
        <div class="block-title">
            <strong><span>Reviews</span></strong>
        </div>
        <div class="block-content">
        <p><a href="<?php echo $this->getMacGuffin($this->getProductId()) ?>">MacGuffin!</a></p>
        </div>
    </div>
    

    Edit app/code/core/Mage/Review/Block/Product/View/List.php and add the helper URL function before the class closing brace:

        public function getMacGuffin($id)
        { return Mage::getUrl('review/product/list', array('id'=> $id));
        }
    

    Now sort out your layout XML add to the reference left block or reference right, whatever, for the product page directives:

    <block type="review/product_view_list" name="review_sidebar" as="macguffin" template="review/sidebar.phtml"/>
    

    That gets you the link you wanted, in a nice sidebar block with some stuff you can CSS to. Copy it over to your main theme. You can take what you want from the list.phtml template I pointed you towards earlier and do your own code to summarize your reviews or say something else if you have no reviews.