Question

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

Was it helpful?

Solution

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);

OTHER TIPS

Firstly, you need all products collection then you get it product list as per manufacture. Try This

   $name = 'brand';
$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);

$specificbrand = array();
foreach ($attributeOptions as $_option)
{
    $productcollection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter($name, $_option['value']);
    foreach ($productcollection as $_product)
    {
        if($_option['label'] == 'specificbrand')
        {
            $specificbrand[] = $_product->getSku();
        }
    }
} 

print_R($specificbrand);

Use below code to do the same -

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

Then you just have to loop through and get details -

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

Make sure the attribute is added in Attribute set.

  • First crosscheck your set manufacturer id on the products that you want to fetch.
  • After by viewing your code it seems you used echo, rather you should use var_dump() or print_r() for printing array.

    Otherwise the code seems correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top