3

How can I sort orders by newest? Currently I'm filtering by complete and canceled status and I want to sort results by latest ordered date or by latest order ID. Now it show to me by the first ID lets say 10000001 but i want it to be 1000010

$collection = Mage::getModel('sales/order')->getCollection()                            
    ->addFieldToFilter('status', array('nin' => array('canceled','complete')))                       
    ->addFieldToFilter('state',Array('eq'=>Mage_Sales_Model_Order::STATE_NEW))                        
    ->addAttributeToSelect('*'); 
3

I believe you are looking for the addAttributeToSort() method:

$collection = Mage::getModel('sales/order')->getCollection()
    ->addFieldToFilter('status', array('nin' => array('canceled', 'complete')))
    ->addFieldToFilter('state', array('eq' => Mage_Sales_Model_Order::STATE_NEW))
    ->addAttributeToSelect('*')
    ->addAttributeToSort('increment_id', 'DESC')
;

foreach ($collection as $item) {
    echo $item->getIncrementId();
}

You can edit the attribute (and direction) on which the collection has to be sorted on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.