0

I am trying to do the same thing in Magento CE 1.9.1.0 . 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 />";
1
  • My issue is no products are appearing at all. What I want to achieve is where the "related products" appear on the product detail page I want a listing of 5 products that have the same value for the attrabute named family.
    – Jayreis
    Dec 23, 2014 at 18:22

2 Answers 2

1

First of all you need to set in Attribute settings "Used in product listing" to Yes.

Then you can simple do:

Avoid double load the product, it is already loaded.

    //$itemcollectionname = Mage::getModel('catalog/product')->load($_product->getId())->getAttributeText('family');
$itemcollectionname = $_product->getAttributeText('family');

Or load a new collection like:

$itemcollectionname = Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect('*');

foreach($itemcollectionname as $product){
   echo $product->getFamily();
   //OR
   echo $product->getAttributeText('family');
}

So if you want to filter by family you will need a value to filter by:

Example:

$itemcollectionname = Mage::getResourceModel('catalog/product_collection')
                        ->addAttributeToSelect('*')
                        ->addAttributeToFilter('family', 'Pants');

    foreach($itemcollectionname as $product){
       echo $product->getId();
    }
0

First get the id by label and then perform addField to filter on the collection

$productModel = Mage::getModel('catalog/product');
$attr = $productModel->getResource()->getAttribute("manufacturer");
if ($attr->usesSource()) {

     $manufacturer_id = $attr->getSource()->getOptionId("Nike");
}

$products = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToFilter('manufacturer',$manufacturer_id);

I hope this will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.