3

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

  • it is issue with attribute,Please check attribute is used in Product listing(Goto manage attribute>select manufacture> ckd Used in Listing) – Amit Bera Jan 12 '15 at 12:56
  • i set it to yes but still no change in frontend. – Piyush Jan 12 '15 at 13:08
  • please tell me what you get when you either log or var_dump your $products variable. – easymoden00b Jan 12 '15 at 18:21
4

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);
  • I have tried it also but its not working. – Piyush Jan 12 '15 at 12:45
  • But are you sure there are products for that specific manufacturer? – Marius Jan 12 '15 at 12:59
  • yaa there are some products for that manufacturer. – Piyush Jan 12 '15 at 13:07
  • Is the manufacturer attribute marked 'Use in product listing'? If not, try to set that flag to yes and reindex everything. – Marius Jan 12 '15 at 13:09
  • it is already set to yes. – Piyush Jan 12 '15 at 13:18
0

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

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.

0
  • 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.