Magento 1.7 – Get Products by Manufacturer

magento-1.7manufacturerproduct-attributeproduct-collection

I want to display all the products which belongs to a particular manufacturer.

Here is my code:-

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

echo $products->getItems();

It displays nothing. How can I get this??

Best Answer

Instead of $products->getItems(); try looping through the collection.

foreach ($products as $product) {
    //do something with product
}

But I suggest adding some attributes to the collection. You might get only the id and sku (and a few other uninteresting attribute values) the way you are doing it.

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*') //instead of * you can use an array with the required attributes
    ->addAttributeToFilter($attributeCode, $manufacturerId);