Magento: addAttributeToFilter & Conditions

Examples:

/* Showing all disabled products having  display_date less than equal to current date */


$model = Mage::getModel('catalog/product');


$currentTimestamp = Mage::getModel('core/date')->timestamp(time());
$currentDate = date('Y-m-d', $currentTimestamp);


$collection = $model->getCollection()
   ->addAttributeToSelect(array('entity_id', 'status'))
   ->addAttributeToFilter('status', array('eq' => 2)) // 2- Disabled products
   ->addAttributeToFilter('display_date', array('lteq' => $currentDate));

Note: 'display_date' is a custom attribute.

# Following are the some examples of conditions:

Equals:
$_products->addAttributeToFilter('status', array('eq' => 1));

Not Equals:
$_products->addAttributeToFilter('sku', array('neq' => 'testsku'));

Like:
$_products->addAttributeToFilter('sku', array('like' => 'PG%'));

Not Like:
$_products->addAttributeToFilter('sku', array('nlike' => 'testprod%'));

In:
$_products->addAttributeToFilter('id', array('in' => array(1,3,8)));

Not In:
$_products->addAttributeToFilter('id', array('nin' => array(1,3,8)));

Greater Than:
$_products->addAttributeToFilter('id', array('gt' => 6));

Less Than:
$_products->addAttributeToFilter('id', array('lt' => 6));

Greater Than or Equals To:
$_products->addAttributeToFilter('id', array('gteq' => 6));

Less Than or Equals To:
$_products->addAttributeToFilter('id', array('lteq' => 6));

Note: You will know more about Magento & Download latest version of Magento from http://www.magentocommerce.com/
Magento: addAttributeToFilter & Conditions Magento: addAttributeToFilter & Conditions Reviewed by Pranay Gondane on 4:01:00 AM Rating: 5

No comments

Free! Free!Free! Subscribe to Get Free PHP (Magento) tutorial Update in Your Inbox!!! Hurry Up!!!