Ghi đè _prepareCollection () của khách hàng Magento không hoạt động


8

Tôi đã ghi đè phương thức _prepareCollection () của Mage_Adminhtml_Block_Customer_Grid và thêm các dòng sau

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

đến:

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

và thêm 3 cột này vào _prepareColumns()như sau

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

Vấn đề là 3 cột này không được điền dữ liệu khi nó nằm trong mô đun overriden và nếu tôi thêm cùng một mã trong lõi thì 3 cột này sẽ được điền với các giá trị

Câu trả lời:


17

Cuối cùng, bạn gọi phương thức ban đầu bằng return parent::_prepareCollection();

Nó sẽ chuẩn bị bộ sưu tập một lần nữa với các tham số mặc định và ghi đè lên của bạn.

Gọi cho cha mẹ

return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();

thay vì

return parent::_prepareCollection();

Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookie và Chính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.