Friday, 10 June 2011

Magento addAttributeToFilter

Get all product collection with the below code:
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('*');
 
Add different types of filters  on collection. example shown below
//  Equal To (eq)
$collection->addAttributeToFilter('status', array('eq' => 1));

//  Not Equal To (neq)
$collection->addAttributeToFilter('visibility', array('neq' => 1));

// Greater Than (gt)
$collection->addAttributeToFilter('price', array('gt' => 3.99));

// Less Than (lt)
$collection->addAttributeToFilter('price', array('lt' => 3.99));

// Greater Than or Equal To (gteq)
$collection->addAttributeToFilter('price', array('gteq' => 4.99));

// Less Than or Equal To (lteq)
$collection->addAttributeToFilter('price', array('lteq' => 4.99));

// Contains (like) - also uses % wildcards
$collection->addAttributeToFilter('sku', array('like' => 'DVD%'));

// Does Not Contain (nlike) - also uses % wildcards
$collection->addAttributeToFilter('sku', array('nlike' => 'ABC%'));

// In Array (in)
$collection->addAttributeToFilter('id', array('in' => array(1,3,12)));

// Not In Array (nin)
$collection->addAttributeToFilter('id', array('nin' => array(1,2,12)));

// Is NULL (null)
$collection->addAttributeToFilter('description', 'null');

// Is Not NULL (notnull)
$collection->addAttributeToFilter('description', 'notnull');

1 comment:

  1. Hello, I know this post is old, but I thought I should give a try. I am interested in filtering by price, ex:

    $collection->addAttributeToFilter('price', array('gteq' => 4.99));

    It works when the currency is set to default, but if I change the currency, this filter applies also to the default price. Is there any solution for this?

    ReplyDelete