PHP Magento Tips

Adding New Column to Customers Grid in Magento Admin with Custom Module

Rate this posting:
{[['']]}

Pretty often we face a task, where we need to add a custom column to the customers/orders grid in Magento admin. The solution is simple and if you google for it, you will get tons of solutions. However, pretty much all of them describe the same way, which isn’t always proper. Nick described the simple and trivial way of adding columns in this article Adding a Custom Attribute or Column to the Sales Orders Grid in Magento admin and you are always able to follow it, except the cases, when some other extension already overrides the _prepareColumns() method. At that point, we need some universal solution that won’t conflict with other extensions that may potentially be overwriting the grid block. So, let’s use an observer for our task. First of all, you need to add your own extension for registering the observer.

Step 1: add below code in app/etc/modules/Mycompany_CustomGrid.xml.

<?xml version="1.0"?>
<config>
    <modules>
        <Mycompany_CustomGrid>
            <active>true</active>
            <codePool>local</codePool>
        </Mycompany_CustomGrid>
    </modules>
</config>

Step 2: add below code in app/code/local/Mycompany/CustomGrid/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Mycompany_CustomGrid>
            <version>0.1.0</version>
        </Mycompany_CustomGrid>
    </modules>
    <global>
        <models>
            <mycompany_customgrid>
                <class>Mycompany_CustomGrid_Model</class>
            </mycompany_customgrid>
        </models>
    </global>
    <adminhtml>
        <events>
            <core_block_abstract_prepare_layout_before>
                <observers>
                    <customgrid_column_append>
                        <type>model</type>
                        <class>Mycompany_CustomGrid_Model_Observer</class>
                        <method>appendCustomColumn</method>
                    </customgrid_column_append>
                </observers>
            </core_block_abstract_prepare_layout_before>
        </events>
    </adminhtml>
</config> 

Step 3: add below code in app/code/local/Mycompany/CustomGrid/Model/Observer.php

<?php 
class Mycompany_CustomGrid_Model_Observer extends Varien_Event_Observer
{
    public function appendCustomColumn(Varien_Event_Observer $observer)
    {
        $block = $observer->getBlock();
        if (!isset($block)) {
            return $this;
        }
    
        if ($block->getType() == 'adminhtml/customer_grid') {
            /* @var $block Mage_Adminhtml_Block_Customer_Grid */
            $block->addColumnAfter('middlename', array(
                    'header'    => 'Middle Name',
                    'type'      => 'text',
                    'index'     => 'middlename',
            ), 'email');
        }
    }
}
?>

As you can see, the observer retrieves the customer grid block and executes its method addColumnAfter for adding a new column. The column type is “text” and the index is “middlename”. “Middlename” – it’s standard customer’s attribute in Magento, but you are able to add any custom attribute using this way. The advantage of this method: your code won’t conflict with other third party solutions that also add custom columns. You can use the observer for adding a column to almost any grid in Magento admin. All you need is to change the following condition:

$block->getType() == 'adminhtml/customer_grid')

Depending on your grid block. For example, order’s grid block is adminhtml/sales_order_grid.
Good luck and do not hesitate to ask your questions in comments below :)
Tag : Magento
0 Comments On "Adding New Column to Customers Grid in Magento Admin with Custom Module"

Back To Top