Question

I am displaying related products in content block of layout in product view page. My code in catalog.xml is:

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

Now, all the related products are displayed in my product view page, but I want to display only 3 products, What should I do? Please, anybody can help ??

Was it helpful?

Solution 2

You need to edit the template file catalog/product/list/related.phtml and limit the loop to only iterate through 3 products.

For example:

    ...
    <?php $i = 0; ?>
    <?php foreach($this->getItems() as $_item): ?>
        <?php if($i++ == 3) break; ?>
        ...

OTHER TIPS

This worked for me. You have to limit the query, and maybe shuffle it before loading the collection. Open the file \app\code\core\Mage\Catalog\Block\Product\List\Related.php and look for the method:

Mage_Catalog_Block_Product_List_Related->_prepareData()

Add this line before the load() call.

$this->_itemCollection->getSelect()->limit(3)->order(new Zend_Db_Expr('RAND()'));

It is still not pretty, !!you should not write in the core code!!, but it's a good start.

You have a collection of products prepared in Mage_Catalog_Block_Product_List_Related and it is iterated in the template file catalog/product/list/related.phtml. If you have any rule that should add to limit your collection, you can override the _prepareData() method and modify the collection. If not, the easiest solution is to exit from the loop in the phtml after three iterations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top