Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between addAttributeToFilter and addFieldToFilter in Magento?

Tags:

php

magento

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();
like image 753
user2700690 Avatar asked Mar 05 '14 20:03

user2700690


1 Answers

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));
like image 121
sebi Avatar answered Sep 30 '22 07:09

sebi