Add Custom Attribute To Customer Grid

In the previous blog we have discussed on adding a custom attribute to cutomer. Now, if we do need to show that attribute in the Customer Grid then we need to follow the steps below:

Step: 1. Override the Mage_Adminhtml_Block_Widget_Grid

class Excellence_Subcust_Block_Adminhtml_Customer_Grid extends
Mage_Adminhtml_Block_Customer_Grid {
    public function setCollection($collection) {
        $collection->addAttributeToSelect('custom_attr');
        $this->_collection = $collection;
    }
    protected function _prepareColumns() {
        $this->addColumnAfter('custom_attr', array(
            'header' => Mage::helper('customer')->__('Customer Code'),
            'type' => 'text',
            'index' => 'custom_attr'
        ), 'entity_id');
        return parent::_prepareColumns();
    }
}

Step: 2. Edit your config.xml

<global>
    <blocks>
        <adminhtml>
            <rewrite><customer_grid>Excellence_Subcust_Block_Adminhtml_Customer_Grid</customer_grid>
            </rewrite>
        </adminhtml>
    </blocks>
</global>

Explaination:
In Step 1 we had not overridden the _prepareCollection() because calling return parent::_prepareCollection(); will reset the collection to default magento implementation and searching will not work for custom attribute column.
For this we had overridden the setCollection() and added our custom attribute to select. You can change it as per your requirement.

  • Kristi Guerrero

    Code runs successfully.