0

I am trying to sort the items based on the order they were added to the cart (oldest at the top, each item added under that). Is there any logical method to the way cart items are sorted in Magento 1.5? I can't seem to find a pattern!

I've been trying to sort using this on /app/design/frontend/default/mytheme/template/checkout/cart.phtml

$cartitems = Mage::getModel('sales/quote_item')->getCollection()->setQuote($this->getQuote())->addFieldToSelect('updated_at')->addOrder('updated_at', 'asc');

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

And then looping though the items per usual...

<?php foreach($items as $_item): ?>
    <?php echo $this->getItemHtml($_item) ?>
<?php endforeach ?>

I also need to sort cart items on a mini cart that drops down when you hover over a cart link in the header. It's working great except the ordering.

$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();

I've tried a bunch of different ways to sort, and just when I think I have it, all I need to do is change the qty of one of the items and they are all out of order again.

Can someone please point me in the right direction? I have found a couple things to try but nothing seems to be working.

1

If you are looking for the data that the item was added to the cart you could try created_at on the table sales_falt_quote_item

$cartitems = Mage::getModel('sales/quote_item')->getCollection()->setQuote($this->getQuote())
    ->addFieldToSelect('created_at')->addOrder('created_at', 'asc');
2

A quick way out:

app/code/core/Mage/Sales/Model/Quote.php

public function getItemsCollection($useCache = true)
    {
        if (is_null($this->_items)) {
            $this->_items = Mage::getModel('sales/quote_item')->getCollection()**->setOrder('updated_at','desc');**
            $this->_items->setQuote($this);
        }
        return $this->_items;
    }

You can always create a local file so that the core files are not overwritten.

  • I have that in place (local file), but it seems if I change the qty of one of the items in the cart, they change ordering. Are there other options other than updated_at? – jyoseph Jun 25 '14 at 14:48

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.