Magento客户网格_prepareCollection()覆盖无效


8

我已经覆盖了Mage_Adminhtml_Block_Customer_Grid的_prepareCollection()方法,并添加了以下几行

->addAttributeToSelect('cus_city')
->addAttributeToSelect('cus_country')
->addAttributeToSelect('cus_state')

至:

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();
}

并添加这些3列在_prepareColumns()如下

$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,
));

问题是,当这3列位于重写模块中时,此3列未填充数据;如果我在核心中添加相同的代码,则这3列将填充值

Answers:


17

最后,您使用 return parent::_prepareCollection();

它将使用默认参数再次准备集合,并覆盖您的默认参数。

打电话给祖父母

return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();

代替

return parent::_prepareCollection();

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.