Sort Orders by Customer Groups

Last modified by Ben B Incani on Fri, May 11, 2012 00:19
Source|Old Revisions  


This is a quick tutorial on how to sort orders by Customer Group in the “admin/sales_order/” section. I really had a hard time figuring it out so I thought I would share my experience.

Step 1

Go to /app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php and add the following code in the _prepareColumns() method (around line 60):

        $this->addColumn('customer_group_id', array(
            'header'=> Mage::helper('customer')->__('Customer Group'),
            'width' => '80px',
            'index' => 'customer_group_id',
            'renderer' => new Mage_Adminhtml_Block_Sales_Order_Renderer_CustomerGroup(),
            'type' => 'options',
            'options' => Mage_Adminhtml_Block_Sales_Order_Renderer_CustomerGroup::getCustomerGroupsArray(),

Step 2

We now need to create our renderer class which will transform group IDs into apropriate labels:

Create the following file: /app/code/core/Mage/Adminhtml/Block/Sales/Order/Renderer/CustomerGroup.php (you’ll also have to create the Renderer directory)

and put the following code in it:

class Mage_Adminhtml_Block_Sales_Order_Renderer_CustomerGroup
extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {

    // Holds an associative array with customer_group_id and the associated label
    private static $_customerGroups = array(); // "singleton"

    public static function getCustomerGroupsArray() {
        // Make sure the static property is only populated once
        if (count(self::$_customerGroups) == 0) {
            $customer_group = new Mage_Customer_Model_Group();
            $customer_groups = $customer_group->getCollection()->toOptionHash();
            self::$_customerGroups = $customer_groups;

        return self::$_customerGroups;

    // Transforms the customer_group_id into corresponding label
    public function render(Varien_Object $row)
        $val = $this->_getValue($row);
        $customer_groups = self::getCustomerGroupsArray();
        return isset($customer_groups[$val]) ? $customer_groups[$val] : false;


Step 3

You can now visualize/sort your orders by customer groups.

Tested version(s)

Magento 1.0

Magento ver.

Add the group_id to the collection app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php


You also need at override the way NULL values are treated.

    protected function _addColumnFilterToCollection($column) {
        if ($this->getCollection()) {
            $field = ( $column->getFilterIndex() ) ? $column->getFilterIndex() : $column->getIndex();
            if ($column->getFilterConditionCallback()) {
                call_user_func($column->getFilterConditionCallback(), $this->getCollection(), $column);
            else {
                $cond = $column->getFilter()->getCondition();
                if ($field && isset($cond)) {
                    if (in_array('NULL', array_values($cond))) {
                        $this->getCollection()->addFieldToFilter($field, array('null' => true));
                    else {
                        $this->getCollection()->addFieldToFilter($field, $cond);
        return $this;