How To Sort Cart Items in Magento

/* Fetching the updated cart items */
$items = array();

$cart = Mage::getModel('sales/quote_item')->getCollection()
->setQuote(Mage::getModel('checkout/cart')->getQuote())
->addFieldToSelect('*')
->addOrder('updated_at', 'desc');      

foreach ($cart as $item)
{
if (!$item->isDeleted() && !$item->getParentItemId()) {
$items[] = $item;
}

Now you are ready to use $items object.

If you want to use this $items object in cart then

Replace:
foreach($this->getItems() as $_item):

With:
foreach($items as $_item):

And you will get the sorted cart items by last modified date in descending order.
How To Sort Cart Items in Magento How To Sort Cart Items in Magento Reviewed by Pranay Gondane on 1:46:00 AM Rating: 5

2 comments

  1. That's a great help you did for me. I was stuck in this problem and thank God that I stumbled upon this post. You have saved my time.

    Regards,
    Prasant Saxena
    Software Development Company

    ReplyDelete
    Replies
    1. You are most welcome Prasant!!! You can also find some other interested magento tips and tricks in my blog... http://pranaydac08.blogspot.in/

      Delete

Free! Free!Free! Subscribe to Get Free PHP (Magento) tutorial Update in Your Inbox!!! Hurry Up!!!