Friday, September 7, 2012

Magento Add attribute Filter

Magento has filterable condition which help to sort out the result.Let Look How it works

addAttributeToFilter is a function that can be called on a product collection in Magento. it adds a condition to the WHERE part of the MySQL query used to extract a product collection from the database


For Example:

$_products = Mage::getModel('catalog/product')->getCollection()

            ->addAttributeToSelect(array('name', 'product_url', 'small_image'))

            ->addAttributeToFilter('sku', array('like' => 'UX%'))

            ->load();

The above code would get a product collection, with each product having it's name, url, price and small image loaded in it's data array. The product collection would be filtered and contain only products that have an SKU starting with UX.

so here Filter Condition have 'sku' with 'like' opertor which will help to filter result based on SKU

There are More Filterable Condition which we can used while filtering

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

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

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

Note :One thing to note about like is that you can include SQL wildcard characters such as the percent sign.

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

In - in
$_products->addAttributeToFilter('id', array('in' => array(1,4,74,98)));

Note:When using in, the value parameter accepts an array of values.

Not In - nin 
$_products->addAttributeToFilter('id', array('nin' => array(1,4,74,98)));

NULL - null 
$_products->addAttributeToFilter('description', 'null');

Not NULL - notnull
$_products->addAttributeToFilter('description', 'notnull');

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

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

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

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

Hope this Will Help You!

No comments:

Post a Comment

Thankyou for your Comments

LinkWithin

Blog has moved, searching new blog...