Home » Magento23 December 2010

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

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:-

Hope this helps. Thanks.

Magento

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • 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’));
     

  • Pronay Santra

    How to get product collection by category and filter by associated product attributes…

    hare is my code not working….

    $collection = Mage::getModel(‘catalog/category’)
    ->load(3)
    ->getProductCollection()
    ->addAttributeToSelect(‘*’);
    $collection->addAttributeToFilter(‘color’,array(
    array(‘in’=> array(‘red’, ‘green’)),
    ));