Search code examples
phpmagentoattributesmagento-1.9

Magento - List products with a specific attribute


How can we list in Magento, products whose attribute, which I defined myself "generatepdf" is equal 1 ?

I wrote the following code :

    $collection = Mage::getModel('catalog/product')->getCollection();
    $collection->addAttributeToSelect('*');
    $collection->addFieldToFilter(array(
        array('name'=>'generatepdf','eq'=>'1')
    ));
    foreach ($collection as $product) {
        var_dump($product->getData());
    }

But I get the following error:

    Fatal error: Call to a member function getBackend() on a non-object in /var/www/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 816

My code isn't good or is there a specific method to set the attributes, so they can be used in this type of request ?


Solution

  • $collection = Mage::getModel('catalog/product')->getCollection();
    $collection->addAttributeToSelect('*');
    $collection->addAttributeToFilter('generatepdf','1');