Question

I am wondering if it's possible to specify multiple sorting conditions for the order grid in the Admin Panel.

Let's say for example I wanted to sort all orders first by their Bill to Name and then the Purchased On column. Any ideas how that can be done?

I have looked at the code but I got stuck at the Varien_Data_Collection_Db::_setOrder method, with no idea how to implement a solution there:

private function _setOrder($field, $direction, $unshift = false)
{
    $this->_isOrdersRendered = false;
    $field = (string)$this->_getMappedField($field);
    $direction = (strtoupper($direction) == self::SORT_ORDER_ASC) ? self::SORT_ORDER_ASC : self::SORT_ORDER_DESC;

    unset($this->_orders[$field]); // avoid ordering by the same field twice
    if ($unshift) {
        $orders = array($field => $direction);
        foreach ($this->_orders as $key => $dir) {
            $orders[$key] = $dir;
        }
        $this->_orders = $orders;
    } else {
        $this->_orders[$field] = $direction;
    }
    return $this;
}
Was it helpful?

Solution

Never mind, I figured it out. I can just call Varien_Data_Collection_Db::setOrder twice, e.g.:

$collection->setOrder('bill_to', 'DESC');
$collection->setOrder('created_at', 'ASC');

This example would first sort the collection by the bill to attribute and then the created at attribute.

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