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

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

6 months ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

6 months ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

6 months ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

6 months ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

6 months ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

6 months ago