Magento customer grid _prepareCollection() override is not working
-
13-12-2019 - |
Question
I had override _prepareCollection() method of Mage_Adminhtml_Block_Customer_Grid and added following lines
->addAttributeToSelect('cus_city')
->addAttributeToSelect('cus_country')
->addAttributeToSelect('cus_state')
to:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('created_at')
->addAttributeToSelect('group_id')
->addAttributeToSelect('cus_city') // added
->addAttributeToSelect('cus_country') // added
->addAttributeToSelect('cus_state') // added
->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();
}
and add these 3 columns in the _prepareColumns()
as follows
$this->addColumn('cus_city', array(
'header' => Mage::helper('customer')->__('City'),
'width' => '100',
'index' => 'cus_city'
));
$this->addColumn('cus_country', array(
'header' => Mage::helper('customer')->__('Country'),
'width' => '100',
'index' => 'cus_country'
));
$data_array=array();
$statearray = Mage::getModel('directory/region')->getResourceCollection() ->addCountryFilter('IN')->load()->toOptionArray();
foreach($statearray as $states){
$data_array[$states['value']] = $states['label'];
}
$this->addColumn('cus_state', array(
'header' => Mage::helper('customer')->__('State'),
'width' => '100',
'type' => 'options',
'index' => 'cus_state',
'options' => $data_array,
));
Issue is this 3 columns are not populating with the data when it is in the overriden module and if i add same code in the core this 3 columns are populating with the values
Solution
In the end you call the original method with return parent::_prepareCollection();
It will prepare the collection again with the default parameters and override yours.
Call the grand parent
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
instead of
return parent::_prepareCollection();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange