Magento-1.9 – Get Product List by Manufacturer Name or Option ID

attributescollection;magento-1.9product-list

i am trying this code

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

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

    ?>

but get real solution

output of this code is

Array
( 
)

how can i get product list by manufacturer name

Best Answer

Here is one way to do it where you cycle through the options of the attribute and get a product collection for each one:

$name = 'manufacturer';
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter($name)->getFirstItem();
$attributeId = $attributeInfo->getAttributeId();
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions(false);

foreach ($attributeOptions as $_option){
    echo $_option['label'];

    $productcollection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter($name, $_option['value']);

    foreach ($productcollection as $_product){
        echo $_product->getName();
    }
}

This way is a bit slow so you may prefer to instead load a collection of all products and then sort them by the manufacturer attribute.

Related Topic