15

This is how i'm getting the cart items:

$quote = Mage::getModel('checkout/cart')->getQuote();
$items=$quote->getAllitems();

I'd like to do something like this:

$items->sortBy('updated_at','desc');

What is the proper magento way to do this?

I'm tempted to do something like this:

$productArray=array();
foreach($items as $item){
    $product=$item->getProduct();
    array_push($productArray,$product);
}
$productArray = $this->sortArray($productArray);

protected sortArray($productArray){
    ...sort by updated date;
    return $sortedArray
}

However i'd need to created a bunch of dateTime objects in order to easily compare these values and this seems to be a bit of a computationally cumbersome operation.

  • Did you try it? – Marius Apr 30 '15 at 12:43
  • 1
    @Marius yes, the page stops rendering at this point. – easymoden00b Apr 30 '15 at 12:47
  • check the var/log for errors or enable the error reporting. – Marius Apr 30 '15 at 12:48
  • @Marius I'm getting the ever so anomalous: a:5:{i:0;s:59:"Mage registry key "_singleton/core/resource" already exists";i:1;s:763:"#0 /opt/magento/app/Mage.php(225): Mage::throwException('Mage registry k...').. – easymoden00b Apr 30 '15 at 12:53
  • 1
    answer updated .please check. – Amit Bera May 5 '15 at 10:57
3
+50

There have no function to sort the by

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 seOrder 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;
    }

}
  • Fatal error: Call to undefined method Mage_Sales_Model_Resource_Quote_Item_Collection::addAttributeToSort()... – easymoden00b Apr 30 '15 at 13:00
  • wait i will check. – Amit Bera Apr 30 '15 at 13:00
  • I'm tempted to do something like [see updated answer] but that's just a bunch more code that's slightly unwieldy and not very magento-friendly. – easymoden00b Apr 30 '15 at 13:05
0

Solution:

Rewrite your method Mage_Sales_Model_Quote::getAllVisibleItems with it:

public function getAllVisibleItems()
{
    $items = array();
    foreach ($this->getItemsCollection() as $item) {
        if (!$item->isDeleted() && !$item->getParentItemId()) {
            $timeStamp = Mage::getModel('core/date')->timestamp($item->getData('updated_at'));
            $items[$timeStamp] =  $item;
        }
        ksort($items);
    }
    return $items;
}

Written early:

Probably it's not very good solution in this situation, but keep it in mind. (Magento have many such places where it can be usefull)

public function getAllItems()
{
    $items = array();
    foreach ($this->getItemsCollection() as $item) {
        if (!$item->isDeleted()) {
            $items[$item->getData('...')] =  $item;
        }
    }
    ksort($items)

    return $items;
}

Inside getData() method put anyfield you like to sort. In your case it can be updated_at. In case of inserting updated_at better insert its timestamp.

  • by timestamp I assume you mean the instantiation of a DateTime object (of which comparators work upon)? – easymoden00b Apr 30 '15 at 13:58
  • You need to experiment. Maybe it can sort when you put there updated_at – zhartaunik Apr 30 '15 at 14:00
  • Okay, i'll give it a try... now the fun begins of creating a complex module just to use a single function within a single template file.. magento is so stupid sometimes :c like 5 files for one function call.. – easymoden00b Apr 30 '15 at 14:01
  • Following this pattern I sorted categories in menu. Runs like clockwork – zhartaunik Apr 30 '15 at 14:03
  • if two items have the same updated time then they will overwrite one another. – easymoden00b Apr 30 '15 at 14:22

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.