Tôi cần xóa một số cột mặc định và thêm các cột bổ sung vào lưới khách hàng. Tôi quyết định làm cho các cột có thể cấu hình. Đầu tiên tôi đã thêm 2 hộp đa lựa chọn trong system.xml :
<config>
<sections>
<extendedcustomer translate="label" module="extendedcustomer">
<label>Extended Customer</label>
<tab>customer</tab>
<sort_order>100</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<groups>
<manage_grid translate="label">
<label>Manage Customer Grid in Backend</label>
<frontend_type>text</frontend_type>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<remove translate="label">
<label>Remove Columns</label>
<frontend_type>multiselect</frontend_type>
<source_model>extendedcustomer/source_gridColumn</source_model>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</remove>
<add translate="label">
<label>Add Columns</label>
<frontend_type>multiselect</frontend_type>
<source_model>extendedcustomer/source_attributeCode</source_model>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</add>
</fields>
</manage_grid>
</groups>
</extendedcustomer>
</sections>
</config>
Các mô hình nguồn là thẳng về phía trước:
class SomeCo_ExtendedCustomer_Model_Source_GridColumn
{
public function toOptionArray()
{
return [
['value' => 'name', 'label' => 'Name'],
['value' => 'email', 'label' => 'Email'],
['value' => 'group', 'label' => 'Group'],
['value' => 'billing_telephone', 'label' => 'Telephone'],
['value' => 'billing_postcode', 'label' => 'ZIP'],
['value' => 'billing_country_id', 'label' => 'Country'],
['value' => 'billing_region', 'label' => 'State/Province'],
['value' => 'customer_since', 'label' => 'Customer Since'],
['value' => 'website_id', 'label' => 'Website'],
['value' => 'action', 'label' => 'Action']
];
}
}
Mô hình nguồn thứ hai
class SomeCo_ExtendedCustomer_Model_Source_AttributeCode
{
public function toOptionArray()
{
$collection = Mage::getResourceModel('customer/attribute_collection')
->addFieldToSelect(['attribute_code', 'frontend_label'])
->addFieldToFilter('frontend_label', ['notnull'=>'notnull'])
->addFieldToFilter('is_user_defined', 1);
$options = [];
foreach ($collection as $item) {
$options[] = [
'value' => $item->getData('attribute_code'),
'label' => $item->getData('frontend_label')
];
}
return $options;
}
}
Sau đó ghi đè lớp lưới:
class SomeCo_ExtendedCustomer_Block_Adminhtml_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{
public function __construct()
{
parent::__construct();
if ($remove = Mage::getStoreConfig('extendedcustomer/manage_grid/remove')) {
$remove = explode(',', $remove);
} else {
$remove = false;
}
$this->setRemoveList($remove);
if ($add = Mage::getStoreConfig('extendedcustomer/manage_grid/add')) {
$add = explode(',', $add);
} else {
$add = false;
}
$this->setAddList($add);
}
protected function _prepareCollection()
{
if ($remove = $this->getRemoveList()) {
$collection = Mage::getResourceModel('customer/customer_collection');
if (!in_array('name', $remove)) {
$collection->addNameToSelect();
}
foreach (['postcode', 'city', 'telephone', 'region', 'country_id'] as $suffix) {
if (!in_array('billing_'.$suffix, $remove)) {
$collection->joinAttribute('billing_'.$suffix, 'customer_address/'.$suffix, 'default_billing', null, 'left');
}
}
} else {
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
//->addAttributeToSelect('email') // static attributes are added by default
//->addAttributeToSelect('created_at')
//->addAttributeToSelect('group_id')
->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');
}
if ($add = $this->getAddList()) {
$collection->addAttributeToSelect($add);
}
$this->setCollection($collection);
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection(); // call grandParent
}
protected function _prepareColumns()
{
$this->addColumn('entity_id', array(
'header' => Mage::helper('customer')->__('ID'),
'width' => '50px',
'index' => 'entity_id',
'type' => 'number',
));
$remove = $this->getRemoveList();
if (!$remove || !in_array('name', $remove)) {
$this->addColumn('name', array(
'header' => Mage::helper('customer')->__('Name'),
'index' => 'name'
));
}
if ($add = $this->getAddList()) {
$collection = Mage::getResourceModel('customer/attribute_collection')
->addFieldToSelect(['attribute_code', 'frontend_label', 'source_model'])
->addFieldToFilter('attribute_code', ['in' => $add]);
foreach ($collection as $item) {
if ($source = $item->getSourceModel()) {
$this->addColumn($item->getAttributeCode(), array(
'header' => $item->getFrontendLabel(),
'width' => '100',
'index' => $item->getAttributeCode(),
'type' => 'options',
'options' => Mage::getSingleton($source)->toOptionHash(false)
));
} else {
$this->addColumn($item->getAttributeCode(), array(
'header' => $item->getFrontendLabel(),
'width' => '150',
'index' => $item->getAttributeCode()
));
}
}
}
if (!$remove || !in_array('email', $remove)) {
$this->addColumn('email', array(
'header' => Mage::helper('customer')->__('Email'),
'width' => '150',
'index' => 'email'
));
}
if (!$remove || !in_array('group', $remove)) {
$groups = Mage::getResourceModel('customer/group_collection')
->addFieldToFilter('customer_group_id', array('gt'=> 0))
->load()
->toOptionHash();
$this->addColumn('group', array(
'header' => Mage::helper('customer')->__('Group'),
'width' => '100',
'index' => 'group_id',
'type' => 'options',
'options' => $groups,
));
}
if (!$remove || !in_array('billing_telephone', $remove)) {
$this->addColumn('Telephone', array(
'header' => Mage::helper('customer')->__('Telephone'),
'width' => '100',
'index' => 'billing_telephone'
));
}
if (!$remove || !in_array('billing_postcode', $remove)) {
$this->addColumn('billing_postcode', array(
'header' => Mage::helper('customer')->__('ZIP'),
'width' => '90',
'index' => 'billing_postcode',
));
}
if (!$remove || !in_array('billing_country_id', $remove)) {
$this->addColumn('billing_country_id', array(
'header' => Mage::helper('customer')->__('Country'),
'width' => '100',
'type' => 'country',
'index' => 'billing_country_id',
));
}
if (!$remove || !in_array('billing_region', $remove)) {
$this->addColumn('billing_region', array(
'header' => Mage::helper('customer')->__('State/Province'),
'width' => '100',
'index' => 'billing_region',
));
}
if (!$remove || !in_array('customer_since', $remove)) {
$this->addColumn('customer_since', array(
'header' => Mage::helper('customer')->__('Customer Since'),
'type' => 'datetime',
'align' => 'center',
'index' => 'created_at',
'gmtoffset' => true
));
}
if (!$remove || !in_array('website_id', $remove)) {
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('website_id', array(
'header' => Mage::helper('customer')->__('Website'),
'align' => 'center',
'width' => '80px',
'type' => 'options',
'options' => Mage::getSingleton('adminhtml/system_store')->getWebsiteOptionHash(true),
'index' => 'website_id',
));
}
}
if (!$remove || !in_array('action', $remove)) {
$this->addColumn('action',
array(
'header' => Mage::helper('customer')->__('Action'),
'width' => '100',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('customer')->__('Edit'),
'url' => array('base'=> '*/*/edit'),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
}
$this->addExportType('*/*/exportCsv', Mage::helper('customer')->__('CSV'));
$this->addExportType('*/*/exportXml', Mage::helper('customer')->__('Excel XML'));
return Mage_Adminhtml_Block_Widget_Grid::_prepareColumns();
}
}
$block->getCollection()