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.– MufaddalApr 23, 2013 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.– user2064667Apr 23, 2013 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– MufaddalApr 23, 2013 at 4:26
-
I used two columns with left bar.– user2064667Apr 23, 2013 at 4:41
-
Ok so your related products are not display, look for my answer for display related product– MufaddalApr 23, 2013 at 4:44
3 Answers
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')
.
-
1Why 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.– LDusanAug 22, 2014 at 9:15 -
-
1Absolutely, this is just an example. I also didn't include the standard warming against using getmodel in a loop. Aug 22, 2014 at 13:20
-
There's a missing semicolon after
$related_prods = $_product->getRelatedProductIds()
.– shmuliOct 2, 2014 at 2:08 -
@shmuli I have updated this (ancient) answer for a best practice approach and to fix the error. Thanks! Oct 2, 2014 at 2:42
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.
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'); ?>