Adding “Customer Group” column to sales grid in Magento

Adding “Customer Group” column to sales grid in Magento

Surprisingly Magento admin area does not have a “customer group” column under the “Sales > Orders” grid, or under the “Sales > Orders > Create New Order” grid (which is a grid of customers). Why would we need this in a first place? Well, maybe you want to be able to list/filter all the orders in a grid which are purchased by customers from lets say “Dealers” group.
Maybe you want to be able to filter the “Sales > Orders > Create New Order” grid by customer group prior to the actual order creation just to see the total of “Dealers” group customers?

By default “Sales > Orders” grid uses the “sales_flat_order_grid” table for rendering. This table is basically a redundant subset of “sales_flat_order” table. Surprisingly “sales_flat_order” table has the “customer_group_id” column in it, while “sales_flat_order_grid” table does not. So our first step is to add the “customer_group_id” column to the “sales_flat_order_grid” table.

We do so by simple upgrade script that executes the code similar to this:

$installer->getConnection()
        ->addColumn($installer->getTable('sales/order_grid'), 'customer_group_id', array(
            'TYPE' => Varien_Db_Ddl_Table::TYPE_SMALLINT,
            'NULLABLE'  => false,
            'DEFAULT'   => '0',
            'COMMENT' => 'Customer Group'
        ));

If this install/upgrade script executed successfuly we should see the new “customer_group_id” column in the “sales_flat_order_grid” table. Please note that Magento will automatically populate this column from the data in the “sales_flat_order_grid.customer_group_id” once the new orders get saved.

After that we will rewrite the Mage_Adminhtml_Block_Sales_Order_Grid and Mage_Adminhtml_Block_Sales_Order_Create_Customer_Grid but change ony the minimum neceserrr for our new “Customer Group” column to appear.

We do so by adding the proper entries under the config.xml

<global>
<blocks>
    <adminhtml>
        <rewrite>
            <sales_order_create_customer_grid>Mycompany_Myextension_Block_Adminhtml_Sales_Order_Create_Customer_Grid</sales_order_create_customer_grid>
            <sales_order_grid>Mycompany_Myextension_Block_Adminhtml_Sales_Order_Grid</sales_order_grid>
        </rewrite>
    </adminhtml>
</blocks>
</global>

Finally we add the rewrite blocks.

<?php
class Mycompany_Myextension_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
    protected function _prepareColumns()
    {
        $groups = Mage::getResourceModel('customer/group_collection')
            ->addFieldToFilter('customer_group_id', array('gt'=> 0))
            ->load()
            ->toOptionHash();
        $this->addColumn('customer_group_id', array(
            'header'    =>  Mage::helper('customer')->__('Customer Group'),
            'width'     =>  '100',
            'index'     =>  'customer_group_id',
            'type'      =>  'options',
            'options'   =>  $groups,
        ));
        $this->addColumnsOrder('customer_group_id', 'shipping_name');
        return parent::_prepareColumns();
    }
}
<?php
class Mycompany_Myextension_Block_Adminhtml_Sales_Order_Create_Customer_Grid extends Mage_Adminhtml_Block_Sales_Order_Create_Customer_Grid
{
    protected function _prepareColumns()
    {
        $groups = Mage::getResourceModel('customer/group_collection')
            ->addFieldToFilter('customer_group_id', array('gt'=> 0))
            ->load()
            ->toOptionHash();
        $this->addColumn('group', array(
            'header'    =>  Mage::helper('customer')->__('Group'),
            'width'     =>  '100',
            'index'     =>  'group_id',
            'type'      =>  'options',
            'options'   =>  $groups,
        ));
        $this->addColumnsOrder('group', 'email');
        return parent::_prepareColumns();
    }
}

And that’s it. Now you should have the “Customer Group” columns on your “Sales > Orders” and “Sales > Orders > Create New Order” grids.

6
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

CTO / Magento Certified Developer Plus @inchoo. Father, husband!

Other posts from this author

Discussion 6 Comments

Add Comment
  1. dhilip

    Hello Inchoo.net

    Thanks for Nice tutorials in support for Magento Developers.
    Tutorials are excellent to use and improve the skills.

    I have a requirement but no where I found the solution, so I’m posting it here, Please support me…

    My requirement, I want to allow customers with duplicate E-mail Id’s.
    Means same email id can be used by more customers. I’m using a custom module for login with user name.

    Its a very urgent requirement and I’m a very new bee to zend and magento as well, I believe I can expect this article soon.

  2. Hey Branko,

    Thanks for the nice tuts.
    Got some problem making it work.
    It seems that magento doesn’t populate the newly added column customer_group_id for the new orders …

    i quote you -> “Please note that Magento will automatically populate this column from the data in the “sales_flat_order_grid.customer_group_id” once the new orders get saved.”

    I am on a Magento EE 1.8 -> equivalent to a 1.4.2 CE

    Maybe you have a clue for me ?
    Thanks in advance and keep up the good work : )

  3. OK i found out what happened …
    So if others pals have the problem just flush var/cache dir, cause zend generate cache of table structure even if your magento cache is disabled

  4. Kara

    How to use this code?

  5. Milen Petrov

    Curious, why in most of the articles, including this one you always propose extending core classes instead of using event-observer? Surely I do not like this way of extending admin grids as if every module do it managing the conflicts is simply a nightmare…

  6. Hitesh

    Hello Branko,

    I implemented your code, the Customer Group column shows up fine in both Sales_Order_Grid and Sales_Order_Create_Customer_Grid.

    However, the Customer Group column in Sales_Order_Grid is not getting populated.

    I flushed Magento cache, logged out and back in but it’s still showing up empty.

    Can you help me out?
    Thank you very much for the script.

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top