Magento – Showing related products in product view page

magento-1.7productproduct-linking

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.

Best Answer

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').