What’s the difference between addAttributeToFilter and addFieldToFilter in Magento? I created a custom attribute YES/NO. But neither of them is working for me when I tried to pull a product collection. Its not filtering anything.
$Products = Mage::getModel('catalog/product')->getCollection(); $Products->addAttributeToSelect('*'); $Products->addFieldToFilter('amazon_listed',1); $Products->addFieldToFilter('status',Mage_Catalog_Model_Product_Status::STATUS_ENABLED); $Products->load();
addFieldToFilter will filter the products based on columns in the database from the table catalog_product_entity.
addAttributeToFilter will filter the products based on the attributes that you’ve included in your collection.
In your case you must use addAttributeToFilter like this:
$Products->addAttributeToFilter('amazon_listed',array('eq' => 1)); $Products->addAttributeToFilter('status',array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));
addAttributeToFilter() is used to filter EAV collections.
addFieldToFilter() is used to filter Non-EAV collections.
To solve the issue,
use addAttributeToFilter() method and try to set “Used in Product Listing” = Yes for your attribute on Edit attribute page in the Admin panel.
Hope it will help.