This article shows how you can add new column to customer grid in Magento 1.x admin. In this example, we will be adding a fetching the fax field value from customer’s billing address and showing in the customer grid in Magento admin.
For this, you need to rewrite/override the Adminhtml’s customer_grid
block class.
Here is the block override code to be written in your module’s config.xml
file:
YourCompany/YourModule/etc/config.xml
<global>
<blocks>
<yourmodule>
<class>YourCompany_YourModule_Block</class>
</yourmodule>
<adminhtml>
<rewrite>
<customer_grid>YourCompany_YourModule_Block_Adminhtml_Customer_Grid</customer_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
Now, you need to create a new class file that overrides the customer_grid adminhtml class. The code will be adding fax column after zip column in the customer grid.
YourCompany/YourModule/Block/Adminhtml/Customer/Grid.php
<?php
class YourCompany_YourModule_Block_Adminhtml_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{
public function setCollection($collection)
{
// fetching fax from billing address
$collection->joinAttribute('billing_fax', 'customer_address/fax', 'default_billing', null, 'left');
parent::setCollection($collection);
}
protected function _prepareColumns()
{
// adding fax column to the customer grid
$this->addColumn('billing_fax', array(
'header' => Mage::helper('customer')->__('Fax'),
'width' => '150px',
'index' => 'billing_fax',
'type' => 'text',
));
// show the fax column after ZIP column
$this->addColumnsOrder('billing_fax', 'billing_postcode');
return parent::_prepareColumns();
}
}
?>
Now, clear cache from Magento admin (System -> Cache Management
).
That all. Now, you should be able to see two new column in Customer Grid in your Magento admin (Customers -> Manage Customers<\strong>
).
Hope this helps. Thanks.