Pages

Tuesday 24 June 2014

Add Customer Attribute To Customer Grid

In the previous blog we have discussed on adding a custom attribute to customer. 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

<?php
class Easylife_Profile_Block_Adminhtml_Customer_Grid extends    Mage_Adminhtml_Block_Customer_Grid
{

    public function setCollection($collection) {
        $collection->addAttributeToSelect('school');
        $this->_collection = $collection;
    }

    protected function _prepareColumns() {
        $this->addColumnAfter('school', array(
            'header' => Mage::helper('customer')->__('School'),
            'type' => 'text',
            'index' => 'school'
        ), 'entity_id');

        return parent::_prepareColumns();
    }

}

Step: 2. Edit your config.xml

<global>
<adminhtml>
            <rewrite>
                <customer_grid>Easylife_Profile_Block_Adminhtml_Customer_Grid</customer_grid>
            </rewrite>
        </adminhtml>

</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.


Add Customer Attribute To Customer Grid
If you have any queries leave a comment.

No comments:

Post a Comment