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();