Google+

Pages

Friday, August 10, 2012

Magento: Get all related products of current product

Obviously, we can get all related products of current product by calling related.phtml. But there are many cases, in which we can not call related.phtml. For example:let us assume that we are showing two or more products on a page and we may want to show small image and name of these two products at a time.

Many other situations can be there, when we may not have option, but to write custom code to get related products of currently loaded product.

For such situations, try this code:

$collection = Mage::getModel('catalog/product_link')
                    ->getCollection()
                    ->addFieldToFilter('product_id',$product_id)
                    ->addFieldToFilter('link_type_id','1');

$related_products = $collection->getData();

This will give you list of all related products of a product.

Note: Here $product_id is the id of currently loaded product.

That's all.. Cheers.. :)

COURTESY: Mr. Neeraj Gupta (My dear Friend)

1 comment:

Kalem said...

That was what I definitely needed! Thanks.