开发者

Getting the reviews page URL for a product in the product page sidebar [closed]

Closed. This question needs to be more focused. It is not currently accepting answers. 开发者_开发技巧

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 3 years ago.

Improve this question

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?


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.


Does <?php echo $this->getReviewsUrl() ?> work for you?

Otherwise you can always get the product URL like you said, explode it on the ".", insert reviews and stick them back together. I don't think that's a fail-safe solution though, as the review URL's on my Magento installation look very different from the product page URLs, so you might break something during an upgrade. (e.g, my product page looks like domain.com/category/product.htm, but the review page is: domain.com/review/product/list/id/10450/category/281/#review-form)

Edit:

I dug a little deeper. I found the $this->getReviewsUrl() in a helper template at app/design/frontend/base/default/template/review/helper/summary.phtml. That explains why you cannot use the method on the product page itself; it has a different context. The Block helper for this template at app/code/core/Mage/Review/Block/Helper.php has the answer though:

public function getReviewsUrl()
  {
      return Mage::getUrl('review/product/list', array(
         'id'        => $this->getProduct()->getId(),
       'category'  => $this->getProduct()->getCategoryId()
      ));
  }

You can use this function's content in your template to generate the link you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜