如何在magento的後端添加自定義排序選項?我希望能夠通過後端的公司名稱對客戶進行排序。我根本無法弄清楚如何添加排序選項。Magento:在管理部分添加自定義排序選項
我發現了一個clever way to get the company name which can be found here.
如何在magento的後端添加自定義排序選項?我希望能夠通過後端的公司名稱對客戶進行排序。我根本無法弄清楚如何添加排序選項。Magento:在管理部分添加自定義排序選項
我發現了一個clever way to get the company name which can be found here.
你必須覆蓋客戶網要做到這一點,申報你的config.xml文件覆蓋:
<global>
<adminhtml>
<rewrite>
<customer_grid>Namespace_Module_Block_Rewrite_Customergrid</customer_grid>
</rewrite>
</adminhtml>
</global>
然後,你必須在應用程序創建類/代碼/本地/命名空間/模塊/塊/重寫/ Customergrid.php:
<?php
class Namespace_Module_Block_Rewrite_Customergrid extends Mage_Adminhtml_Block_Customer_Grid
{
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->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')
->joinAttribute('company', 'customer_address/company', 'default_billing', null, 'left');
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('entity_id', array(
'header' => Mage::helper('customer')->__('ID'),
'width' => '50px',
'index' => 'entity_id',
'type' => 'number',
));
/*$this->addColumn('firstname', array(
'header' => Mage::helper('customer')->__('First Name'),
'index' => 'firstname'
));
$this->addColumn('lastname', array(
'header' => Mage::helper('customer')->__('Last Name'),
'index' => 'lastname'
));*/
$this->addColumn('name', array(
'header' => Mage::helper('customer')->__('Name'),
'index' => 'name'
));
$this->addColumn('email', array(
'header' => Mage::helper('customer')->__('Email'),
'width' => '150',
'index' => 'email'
));
$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,
));
$this->addColumn('Telephone', array(
'header' => Mage::helper('customer')->__('Telephone'),
'width' => '100',
'index' => 'billing_telephone'
));
$this->addColumn('billing_postcode', array(
'header' => Mage::helper('customer')->__('ZIP'),
'width' => '90',
'index' => 'billing_postcode',
));
$this->addColumn('billing_country_id', array(
'header' => Mage::helper('customer')->__('Country'),
'width' => '100',
'type' => 'country',
'index' => 'billing_country_id',
));
$this->addColumn('billing_region', array(
'header' => Mage::helper('customer')->__('State/Province'),
'width' => '100',
'index' => 'billing_region',
));
$this->addColumn('customer_since', array(
'header' => Mage::helper('customer')->__('Customer Since'),
'type' => 'datetime',
'align' => 'center',
'index' => 'created_at',
'gmtoffset' => true
));
$this->addColumn('company', array(
'header' => Mage::helper('customer')->__('Company'),
'width' => '150px',
'index' => 'company',
));
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',
));
}
$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 parent::_prepareColumns();
}
}
我們延伸Mage_Adminhtml_Block_Customer_Grid和複製功能_prepareColl ection()和_prepareColumns()有兩個關鍵區別。在_prepareCollection(),我們添加:
->joinAttribute('company', 'customer_address/company', 'default_billing', null, 'left')
要加載客戶的公司,然後在_prepareColumns(),我們添加您需要的列:
$this->addColumn('company', array(
'header' => Mage::helper('customer')->__('Company'),
'width' => '150px',
'index' => 'company',
));
創建要被添加到排序的屬性作爲下拉列表下拉列表。然後進入管理>目錄>屬性>管理屬性> [屬性如:'名稱'等>屬性>前端屬性並將「用於產品列表中的排序」標記爲是。該屬性將通過下拉菜單添加到排序中。
我很高興看到您非常詳盡的回覆,但我不知道您要編輯的config.xml文件。你可以給我文件路徑嗎? – AndHeiberg
在這個迴應中,我假設你正在創建一個擴展來執行此操作。你可以修改Magento核心文件嗎? – changeling
是的,雖然升級magento會每次都打破它嗎? – AndHeiberg