I have a requirement where, in product details (view) page, I need to display related products which are having the same color as of the product being viewed.

For example, I'm on a page which shows a "Red" Shoe, I should have a Red T-shirt, Red-Refrigerator, Red anything else to be displayed as related products (maximum 5 products).

I know this can be done easily in Enterprise edition, but how to do this in Community edition?

Any help please?


All products are simple products.

2 Answers 2


You can create a new block (and maybe a new template). Here is how you get the product collection for your needs:

$currentProduct = Mage::registry('current_product');
if ($currentProduct){
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) //add some attribtues to select
        ->addMinimalPrice() //add prices to select
        ->addAttributeToFilter('color', array('eq'=>$currentProduct->getColor());//filter products with the same color
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection); //only active products
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);//only visible products
    $collection->setPage(1,5);//limit to 5 products
    $collection = new Varien_Data_Collection();

Now in $collection you should have the products you need. You can use that in your custom template.


I am trying to do the same thing in Magento CE . Basically I want to edit the related products block to show all products that have the same value for a specific attribute. Basically I have an attribute called family and when you view product abc which belongs to the attribute family 'Pepsi' I want to display all other products that have a value for the family attribute of 'pepsi'

So I am altering the file app/design/frontend/MY_THEME/default/template/catalog/product/list/related.phtml

The first thing I do in this file is get the value of the family attribute by doing the below

            //get the name of the family that the product being viewed is a part of
             $itemcollectionname = Mage::getModel('catalog/product')->load($_product->getId())->getAttributeText('family');

I then tried using the code in the above post like so

             $rcollection = Mage::getModel('catalog/product')->getCollection()
             ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) //add some attribtues to select
             ->addAttributeToFilter('family', array('eq'=>$itemcollectionname);//filter products with the same family
             Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($rcollection); //only active products
             Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($rcollection);//only visible products
            $rcollection->setPage(1,5);//limit to 5 products
            echo "$rcollection<br />";

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.