B-219 Sec-55 Noida, India
+918010221733

Magento Add attribute Filter

You are finding the way to get a list of Magento objects. You will search and look at some collection variable such as

 
$orderCollection = Mage::getResourceModel(‘sales/order_grid_collection’);

– or –
   

$productCollection = Mage::getModel(‘catalog/product’)->getCollection();

then you use a loop to traverse all objects in the collections such as
   

foreach ($orderCollection as $order)

{

    print_r($order->getData());

}

– or –

   

foreach ($productCollection as $product)

{

    print_r($product->getData());

}

But you only want to get an special objects from the collections? The simple question is using the function Varien_Data_Collection_Db::addFieldToFilter($field, $condition) because all the Magento collection objects are inherited instance of class Varien_Data_Collection_Db.
Now you’re still wondering about how to use the function
Varien_Data_Collection_Db::addFieldToFilter($field, $condition)?
Here is sample for it’s usage.

// getting orders which have status = complete or processing

$collection = Mage::getModel(‘sales/order’)->getCollection()

        ->addAttributeToSelect(‘*’)

        ->addAttributeToFilter(

            array(

                array(‘attribute’=>’status’,’eq’=>’complete’),

                array(‘attribute’=>’status’, ‘eq’=>’processing’)

            )

        );

– or –

  

// getting orders which have status = complete only

$collection = Mage::getModel(‘sales/order’)->getCollection()

        ->addAttributeToSelect(‘*’)

        ->addAttributeToFilter(‘status’,array(‘eq’=>’complete’));

Here are the condition keys

  1. Equals: eq
  2. Not Equals – neq
  3. Like – like
  4. Not Like – nlike
  5. In – in
  6. Not In – nin
  7. NULL – null
  8. Not NULL – notnull
  9. Greater Than – gt
  10. Less Than – lt
  11. Greater Than or Equals To- gteq
  12. Less Than or Equals To – lteq
(Visited 47 times, 1 visits today)

Leave a reply

You must be logged in to post a comment.