Tuesday, October 1, 2013

Magento add attribute to filter conditions for products collection

General Product Collection:
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addAttributeToFilter('status',1); //only enabled product
$collection->addAttributeToFilter('visibility',array("neq"=>1)); //except not visible individually
$collection->addAttributeToSelect(array('name','url','small_image')); //add product attribute to be fetched
//$collection->getSelect()->order('rand()');   //uncomment for getting the products in random order
$collection->addStoreFilter();
if(!empty($collection))
{
foreach ($collection as $_product):
echo $_product->getName();   //get product name
endforeach;
}else
{
echo 'There is no products';
}


Get All Products of a category or Product collection filter by the category

$collection = Mage::getResourceModel('catalog/product_collection')
->setStoreId($this->getStoreId())
->addCategoryFilter($category);


$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image'))->addAttributeToFilter('name', array('like' => 'Nok%'))->load();

a) Equals: eq

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'status'))->addAttributeToFilter('status', array('eq' => 1))->load();

b) Not Equals – neq

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image','sku'))->addAttributeToFilter('sku', array('neq' => 'test-product'))->load();

c) In – in

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku')) ->addAttributeToFilter('id', array('in' => array(20,24,100)))->load();

d) Not In – nin

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))->addAttributeToFilter('id', array('nin' => array(20,24,100)))->load();

e) Like – like

$products = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku')) ->addAttributeToFilter('name', array('like' => 'Nok%'))->load();

f) Not Like – nlike

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku')) ->addAttributeToFilter('name', array('nlike' => 'Nok%'))->load();

g) NULL – null

$products = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))   ->addAttributeToFilter('description', 'null') ->load();

h)  Not NULL – notnull

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))->addAttributeToFilter('description', 'notnull')->load();

i) Greater Than – gt

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))->addAttributeToFilter('id', array('gt' => 10))->load();

j) Less Than – lt

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))->addAttributeToFilter('id', array('lt' => 10))->load();

k) Greater Than or Equals To- gteq

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku'))->addAttributeToFilter('id', array('gteq' => 20))->load();

l) Less Than or Equals To – lteq

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name', 'product_url', 'small_image', 'sku')) ->addAttributeToFilter('id', array('lteq' => 20))->load();

No comments:

Post a Comment

Please mention your comments.......