$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
-
Equals: eq
-
Not Equals – neq
-
Like – like
-
Not Like – nlike
-
In – in
-
Not In – nin
-
NULL – null
-
Not NULL – notnull
-
Greater Than – gt
-
Less Than – lt
-
Greater Than or Equals To- gteq
- Less Than or Equals To – lteq