addAttributeToFilter Conditions In Magento



addAttributeToFilter is a predefined function of the Magento that can be called on a product collection of the Magento. We can say, it adds a condition to the WHERE part of the MySQL query of the Magento, used to extract product collection from the database.
$products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image'))
   ->addAttributeToFilter('name', array('like' => 'EWA%'))
    ->load();

In the above example it is a product collection of all the product having it’s name, product url and small image loaded in it’s data array. We can further filters this collection by using the addAttributeToFilter(), I have used the like as EWA%, it will returns the only products that have name starting with EWA. As in the above example I have used the like Operators into the addAttributeToFilter, there are many operators which we can use into the addAttributeToFilter.

1. Equals: eq

$products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'status'))
   ->addAttributeToFilter('status', array('eq' => 1))
    ->load();

2. Not Equals – neq

$products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))
   ->addAttributeToFilter('sku', array('neq' => 'test-product'))
    ->load();

3. In – in

$products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))
   ->addAttributeToFilter('id', array('in' => array(20,24,100)))
    ->load();

4. Not In – nin

$products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))
   ->addAttributeToFilter('id', array('nin' => array(20,24,100)))
    ->load();

5. Like – like

$products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))
   ->addAttributeToFilter('name', array('like' => 'EWA%'))
    ->load();

6. Not Like – nlike

$products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))
   ->addAttributeToFilter('name', array('nlike' => 'EWA%'))
    ->load();

7. NULL – null

$products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))
   ->addAttributeToFilter('description', 'null')
    ->load();

8. Not NULL – notnull

$products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))
   ->addAttributeToFilter('description', 'notnull')
    ->load();

9. Greater Than – gt

$products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))
   ->addAttributeToFilter('id', array('gt' => 10))
    ->load();

10. Less Than – lt

$products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))
   ->addAttributeToFilter('id', array('lt' => 10))
    ->load();

11. Greater Than or Equals To- gteq

$products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))
   ->addAttributeToFilter('id', array('gteq' => 20))
    ->load();

12. Less Than or Equals To – lteq

$products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))
   ->addAttributeToFilter('id', array('lteq' => 20))
    ->load();

Hope this will be helpful to someone, Thanks and enjoy the Magento coding.