I'm trying to add a "Company" column in the Customer Grid.
The Company is a Customer Attribute I've added programatically and stored in the DB in the customer_entity_varchar table.
I've rewritten the Grid like this :
class MyCompany_MyModule_Block_Adminhtml_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{
protected function _prepareColumns()
{
$this->addColumnAfter('company_name', array(
'header' => 'Company Name',
'type' => 'text',
'index' => 'company_name',
), 'email');
parent::_prepareColumns();
}
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('created_at')
->addAttributeToSelect('group_id')
->addAttributeToSelect('company_name')
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
$this->setCollection($collection);
return parent::_prepareCollection();
}
}
There is nothing displayed except when I sort or filter with this new company column. For example if I sort with ID, no companies displayed, if I sort with Company, everything is perfect.
I've tried to replace the "return parent::_prepareCollection();" by "return $this;" : everything is displayed as expected but there is not any filter or sort available.
Where is my problem ?
Thanks.
Best Answer
Here is the solution of my problem : I was extending the wrong class. Instead of Mage_Adminhtml_Block_Customer_Grid, I extend now Mage_Adminhtml_Block_Widget_Grid. The problem here is I need to add all the functions in Mage_Adminhtml_Block_Customer_Grid in my rewritten class.
Please note that this is the same class as Mage_Adminhtml_Block_Customer_Grid, but with only 2 modifications :
addAttributeToSelect in _prepareCollection() function
addColumn in _prepareColumns() function
Like this :