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:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * 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.





Mukesh Chapagain is a graduate of Kathmandu University (Dhulikhel, Nepal) from where he holds a Masters degree in Computer Engineering. Mukesh is a passionate web developer who has keen interest in open source technologies, programming & blogging.