Question

This might seem like a silly questoin so I apologies if it does but I am looking to change the Sort order for Comments history in Magento Sales/Order_View Comments History Tab.

Currently the Comments are listed in Ascending Order but I would like to list them in Descending order without having to touch any Core files of course...

The question I would like to as is Where? and How?

Thanks in advance...

Was it helpful?

Solution

The status history collection is pulled from the Mage_Sales_Model_Order object in a method called getStatusHistoryCollection. That method is defined as:

/**
 * Enter description here...
 *
 * @return Mage_Sales_Model_Entity_Order_Status_History_Collection
 */
public function getStatusHistoryCollection($reload=false)
{
    if (is_null($this->_statusHistory) || $reload) {
        $this->_statusHistory = Mage::getResourceModel('sales/order_status_history_collection')
            ->setOrderFilter($this)
            ->setOrder('created_at', 'desc')
            ->setOrder('entity_id', 'desc');

        if ($this->getId()) {
            foreach ($this->_statusHistory as $status) {
                $status->setOrder($this);
            }
        }
    }
    return $this->_statusHistory;
} 

As you can see in setOrder('created_at', 'desc') by default it is sorted in descending order, and has been since Magento 1.0 (perhaps longer, I don't have access to prior releases).

So, either you mean ASCENDING order (oldest first) OR you have an extension conflict that is rewriting this method. If it's the former - create a model rewrite of Mage_Sales_Model_Order and rewrite only the getStatusHistoryCollection method, fetching the parent and applying a new sort:

public function getStatusHistoryCollection($reload=false)
{
    $_history = parent::getStatusHistoryCollection($reload);
    $_history->setOrder('created_at', 'asc');
    return $_history;
} 

Otherwise, you'll have to track down the offending module / rewrite that's causing your undesired sort order.

Edit:

Apparently I can't read! For posterity I'm keeping the above because it's good stuff anyhow.

The history tab on the sales order view indeed is sorted in ascending fashion because it contains all of the histories for all of the comments on every transaction item (Credit Memo, Invoice, Order, Shipments, etc.). This pseudo-collection assembled in getFullHistory is gathered by shoving all transaction type comments into a large array and calling a custom sort method on it.

To sort this, you need to rewrite the sort function used in the usort method of the class Mage_Adminhtml_Block_Sales_Order_View_Tab_History. It should be as easy as:

private static function _sortHistoryByTimestamp($a, $b)
{
   return -1 * (parent::_sortHistoryByTimestamp($a, $b));
}

Which should force the sort order to the opposite direction. Sorry for any confusion.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top