Sort Orders by Customer Groups

Last modified by Ben B Incani on Thu, May 10, 2012 21:37
Source|Old Revisions  

This is an old revision of the document!


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

$collection->getSelect()->joinLeft(array('ce'=>'customer_entity'),'ce.entity_id=main_table.customer_id',array('IFNULL(ce.group_id, 0) as group_id'));