Magento 1 - sorting cart items by 'updated_at'
There have no function to sort the cart by updated_at
because of getAllItems
coming from array
of object and according to my concept you cannot sort this by field.
public function getAllItems()
{
$items = array();
foreach ($this->getItemsCollection() as $item) {
if (!$item->isDeleted()) {
$items[] = $item;
}
}
return $items;
}
In this case,you can use getItemsCollection().
then use setOrder function sort by any Field
$items=$quote->getItemsCollection()->setOrder('updated_at', 'desc');
foreach ($items as $item) {
if (!$item->isDeleted()) {
//Do your code $item
}
}
Edit:
Rewrite class Mage_Sales_Model_Quote
It will better idea to rewrite class Mage_Sales_Model_Quote
.In this call magento is called getAllItems()
function and getItemsCollection() function
main responsive for get all items.
<global>
<models>
<magento65343>
<class>StackExchange_Magento65343_Model</class>
<resourceModel>magento65343_mysql4</resourceModel>
</magento65343>
<sales>
<rewrite>
<quote>StackExchange_Magento65343_Model_Sales_Quote</quote>
</rewrite>
</sales>
</models>
Rewrite class:
<?php
class StackExchange_Magento65343_Model_Sales_Quote extends Mage_Sales_Model_Quote
{
}
Use setOrder() function for Sort:
The setOrder() function sort the item collection by fields
<?php
class StackExchange_Magento65343_Model_Sales_Quote extends Mage_Sales_Model_Quote
{
public function getItemsCollection($useCache = true)
{
if ($this->hasItemsCollection()) {
return $this->getData('items_collection');
}
if (is_null($this->_items)) {
$this->_items = Mage::getModel('sales/quote_item')->getCollection()->setOrder('updated_at', 'desc');
$this->_items->setQuote($this);
}
return $this->_items;
}
}