Home » Magento

Magento: How to get / filter all products by attribute value?

23 December 2010 Share/Bookmark

Here, I will show you how you can filter or fetch products related to any particular attribute and value.

A simple scenario will be filtering products by manufacturer/brand. Suppose, I want to get all products under ‘Samsung’ manufacturer/brand.

For this, you need the attribute code and attribute value ID for which you are fetching products.

To fetch attribute name and value, you can see my previous post here:- Magento: How to get attribute name and value?

Now, lets move on to the code. Here is how you can do this:-

/**
 * Get all products related to any particular brand
 * Let us suppose that we are fetching the products related to 'Samsung' brand 
 * Let us suppose the Manufacturer ID of Samsung = 3
 */
 
$manufacturerId = 3;
$attributeCode = 'manufacturer';

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

// print all products
echo "<pre>"; print_r($products->getItems()); echo "</pre>";

Hope this helps. Thanks.

From Mukesh Chapagain's Blog, post Magento: How to get / filter all products by attribute value?

email

php magento mukesh chapagain

Get New Post by Email
RSS Feed Subscribe RSS Feed
  • http://www.facebook.com/rinkart Rinkart Osei Asibey

    Hello, how do I get brand/manufacturer? any code or query for this? i did this
    $products = Mage::getModel(‘catalog/product’)->getCollection();
        $products->addAttributeToSelect(‘*’);
        $products->load();
     foreach ($products as $id => $product):
    $output .= ‘
     
       ‘. $product['entity_id'].’

     
    if (isset($attributes[$product['entity_id']])) {
       $output .= ‘
       ‘;
      
       foreach ($attributes[$product['entity_id']] as $attribute => $values)
        $output .= ‘
       
         ‘. $attribute .’
         ‘.  join(‘, ‘, $values) . ‘
        ‘;
      
       $output .= ‘
       ‘;
      }
     
      $output .= ‘
      ‘;
     
     endforeach;

    endif;

  • Mahesh babu Bokkisam

    hi i have one problem

    I want to load the products which have images

    in  my appdesignfrontendmyThemedefaulttemplatecatalogproductlist.phtml

        $_productCollection=$this->getLoadedProductCollection();
       
    after this i have added this code  but no luck, so what i have to do
        //$this->_productCollection->addAttributeToFilter(‘small_image’,array(‘notnull’=>”,’neq’=>’no_selection’));
        //$this->_productCollection->addAttributeToFilter(‘image’,array(‘notnull’=>”,’neq’=>’no_selection’));
        $this->_productCollection->addAttributeToFilter(‘thumbnail’,array(‘notnull’=>”,’neq’=>’no_selection’));