3

I'm working on a website where I'm using Magento 1.7. I want to display the related product of the current product on product view page. I tried many links like these, but none of them worked for me. Please help me on this. I'm newbie to magento.

  • which page layout is used for product view page? generally related product are display in right bar by default on product view page. – Mufaddal Apr 23 '13 at 4:01
  • I don't want that to be display on right bar or left. I want to be displayed on product detail page. that is view.phtml of product. – user2064667 Apr 23 '13 at 4:18
  • Yes i am asking you which page layout is used for product detail page? if you are using 1column layout then you will consider philwinkle answer – Mufaddal Apr 23 '13 at 4:26
  • I used two columns with left bar. – user2064667 Apr 23 '13 at 4:41
  • Ok so your related products are not display, look for my answer for display related product – Mufaddal Apr 23 '13 at 4:44
10

This is built in to most themes and all of the base themes. So I'm guessing what you want is to relocate the related products to another area.

This is possible in a few different ways:

Via layouts

Using layout XML you can provide the following block declaration to any <reference> -

<block type="catalog/product_list_related" name="catalog.product.related" before="-" template="catalog/product/list/related.phtml"/>

This should call and render the block and associated template file.

Roll your own

Why not? You're a capable developer! In this case, you can do something very similar to what you posted in the links above. This particular example is fairly trivial but shows you what may be possible:

Edit

My what a difference a year makes. Don't load models in loops. Bad Phillip, BAD.

$related_prods = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->addAttributeToSelect('name')
                    ->addAttributeToSelect('sku')
                    ->addAttributeToFilter('entity_id',array('in'=>$_product->getRelatedProductIds()));
foreach($related_prods as $related){
    echo $related->getName() . " " . $related->getSku(); 
}

This can be placed anywhere on the product page or view.phtml but best practice dictates that you should place this in a Block method and reference that inside the template. Bottom line, you either need to have $_product available or use Mage::registry('current_product').

| improve this answer | |
  • 1
    Why not load product collection instead? Something like Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('product_id', array('in' => $relatedIds)); Better than loading Model inside a loop IMHO. – LDusan Aug 22 '14 at 9:15
  • Or even easier: $product->getRelatedProductCollection() – LDusan Aug 22 '14 at 9:50
  • 1
    Absolutely, this is just an example. I also didn't include the standard warming against using getmodel in a loop. – philwinkle Aug 22 '14 at 13:20
  • There's a missing semicolon after $related_prods = $_product->getRelatedProductIds(). – shmuli Oct 2 '14 at 2:08
  • @shmuli I have updated this (ancient) answer for a best practice approach and to fix the error. Thanks! – philwinkle Oct 2 '14 at 2:42
1

Check this block in catalog.xml

 <block type="catalog/product_list_related" name="catalog.product.related" before="-" template="catalog/product/list/related.phtml"/>

just put like this if you want to display in your content area

 <reference name="content">
            <block type="catalog/product_list_related" name="catalog.product.related" after="upsell_products" template="catalog/product/list/related.phtml"/>
        </reference>

save file and clear your cache.

| improve this answer | |
0

Remove the reference to the block related_products from view.phtml

<?php //echo $this->getChildHtml('related_products') ?>

Then add this to anywhere you want in view.phtml

<?php echo $this->getBlockHtml('catalog.product.related'); ?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.