Wednesday, September 12, 2012

addAttributeToFilter condition in Magento


>> In Magento Collection we frequently use addAttributeToFilter() to append condition in the filter. Here's a simple example :
       
            $_products = Mage::getModel('catalog/product')->getCollection()
                            ->addAttributeToSelect('*')
                            ->addAttributeToFilter('status', '1');  
    
     // This piece of code is used to fetch products of status enabled in Magento
                               
  The same thing if we write in custom sql, it looks like :
              
            SELECT * FROM `catalog_product_entity` WHERE `status` = '1';
           
     // I've simplified it in above but Mageto doesn't create sql like this. You can see what magento exactly create by this :
    
             echo $_products->getSelect();
           
  We can add all conditions i.e LIKE, IN, NOT IN, IN etc.

         *    To use DATE between condition :

                 $_products->addAttributeToFilter('created_at', array(
                                                                         'from' => '2013-11-01',
                                                                         'to' => '2013-12-02',
                                                                     ));
                $_products->addAttributeToFilter('created_at', array(
                                                                         'from' => '01 November 2013',
                                                                         'to' => '02 December 2013',
                                                                         'date' => true,
                                                                     ));

         *    To use EQUAL condition :
       
                $_products->addAttributeToFilter('status', array('eq' => 1));
               
        *    To use NOT EQUAL condition :
       
                $_products->addAttributeToFilter('status', array('neq' => 1));
               
        *    To use LIKE condition :
               
                $_products->addAttributeToFilter('name', array('like' => 'polo%'));
               
        *    To use NOT LIKE condition :
               
                $_products->addAttributeToFilter('name', array('nlike' => 'polo%'));
               
        *    To use IN condition :
       
                $_products->addAttributeToFilter('id', array('in' => array(25,52,41,28)));
               
        *    To use NOT IN condition :
       
                $_products->addAttributeToFilter('id', array('nin' => array(25,52,41,28)));
               
        *    To use IS NULL condition :
       
                $_products->addAttributeToFilter('description', array('null' => true));
               
        *    To use NOT NULL condition :
       
                $_products->addAttributeToFilter('description', array('notnull' => true));
               
        *    To use GREATER THAN condition :
       
                $_products->addAttributeToFilter('id', array('gt' => 10));
               
        *    To use LESS THAN condition :
       
                $_products->addAttributeToFilter('id', array('lt' => 10));
               
        *    To use GREATER THAN EQUAL condition :
       
                $_products->addAttributeToFilter('id', array('gteq' => 10));
               
        *    To use LESS THAN EQUAL condition :
       
                $_products->addAttributeToFilter('id', array('lteq' => 10));

        *    To use AND/OR condition :
       
                $_products->addAttributeToFilter('name', array('eq' => 'xyz'));
                $_products->addAttributeToFilter('name', array('eq' => 'abc'));
                Above code will use with AND condition
               ===================================
                $_products->addAttributeToFilter(
                  array(
                      array('attribute'=>'name', 'eq'=>'xyz'),
                      array('attribute'=>'name', 'eq'=>'abc')
                    )
                  );
                Above code will use with OR condition