Categories: Magento

How To addAttributeToFilter Conditions In Magento.?

addAttributeToFilter is one of the main function of the Magento from which specific product collection can be called from the database using queries only. You can use write the queries as per the need and result will be then found accordingly. Here is example of addAttributeToFilter Conditions In Magento.

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

The above code will fetch a product collection, with each product having it’s name, url, price and  image loaded in as per the data array. Here we have used the condition like as A% which will fetch the product names starting from the A respectively. There are similar many conditions operator that used in addition with addAttributeToFilter Conditions In Magento.


addAttributeToFilter Conditionals In Magento:-

Magento addAttributeToFilter with conditions of greater than, less than, greater than and equal to, less than and equal to, not equal, like, not like, is not null, is null, equal to. are shown below with code that you can alter to make specific changes as required in your code.

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', 'sku','small_image'))
   ->addAttributeToFilter('sku', array('neq' => 'test-product'))
    ->load();

3. Like – like

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

4. Not Like – nlike

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

5. In – in

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

6. Not in – nin

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

7. NULL – null

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

8. Not Null – notnull

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

9. Greater Than – gt

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

10. Less Than -lt

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

11. Greater Than or Equals To – gteq

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

12. Less Than or Equals To – lteq

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

Rohan pathak

Recent Posts

Exploring the Best Toy Store in Noida: A Paradise for Kids and Collectors

Toys have always played a crucial role in childhood, fueling imagination, creativity, and learning. Whether you're looking for educational playsets,…

1 day ago

A Unique Toy Shopping Experience: The Best Toy Store in Delhi and Across India

Toys are an essential part of childhood, shaping creativity, learning, and fun-filled memories. Whether it’s the soft embrace of plush…

2 days ago

A World of Play: The Best Toy Shopping Experience in Noida

Toys are a gateway to imagination, creativity, and learning. Whether it’s a child’s first plush toy, a set of engaging…

3 days ago

The Ultimate Guide to Finding the Best Toy Shops in Gurgaon

Toys are more than just playthings; they help children explore, learn, and develop essential life skills. From interactive sensory toys…

4 days ago

The Ultimate Toy Shopping Guide: Finding the Best Toy Store in Noida

Toys are more than just playthings; they are tools that foster creativity, motor skills, and cognitive development in children. From…

5 days ago

Exploring the Best Toy Shop in Delhi: A Blend of Fun, Learning, and Creativity

Toys play an integral role in a child’s growth and development. They spark creativity, encourage learning, and provide endless hours…

1 week ago