Reputation: 135
How to get the last order status in Magento?
e.g an order might have multi status but I would like to get the last status order's status. 1.Pending=>2.processing=>3.complete
$myOrder=Mage::getModel('sales/order');
$orders=Mage::getModel('sales/mysql4_order_collection');
$orders->addFieldToFilter('status',array('in'=> array('processing', 'processed', 'pending fullfilment')));
$orders->addFieldToFilter('store_id',array("in" => array('8', '9')));
$orders->addAttributeToSort('created_at', 'asc');
$allIds=$orders->getAllIds();
print_r($allIds).'</br></br>';
foreach($allIds as $thisId) {
...
}
Upvotes: 0
Views: 4396
Reputation: 2446
Should be pretty simple actually:
$status = Mage::getModel('sales/order')->getCollection()
->setOrder('entity_id', Varien_Data_Collection::SORT_ORDER_DESC)
->getFirstItem()
->getStatus();
Note that status
and state
are two slightly different fields, you may want to double check and be sure that you're pulling the right one for your needs.
Upvotes: 0
Reputation: 10114
The two most obvious fields to order your collection by would be entity_id
or created_at
. In almost all cases though, you should see identical results from each…
By entity_id
$status = Mage::getModel('sales/order')->getCollection()
->setOrder('entity_id', Varien_Data_Collection::SORT_ORDER_DESC)
->getFirstItem()
->getData('status')
;
By created_at
$status = Mage::getModel('sales/order')->getCollection()
->setOrder('created_at', Varien_Data_Collection::SORT_ORDER_DESC)
->getFirstItem()
->getData('status')
;
Upvotes: 3