1

如何在 magento 的后端添加自定义排序选项?我希望能够在后端按公司名称对客户进行排序。我根本不知道如何添加排序选项。

我找到了一个聪明的方法来获取可以在这里找到的公司名称。

4

1 回答 1

3

您必须覆盖客户网格才能执行此操作,以在 config.xml 上声明覆盖:

<global>
     <adminhtml>
            <rewrite>
                <customer_grid>Namespace_Module_Block_Rewrite_Customergrid</customer_grid>
            </rewrite>
     </adminhtml>
</global>  

然后你必须在 app/code/local/Namespace/Module/Block/Rewrite/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 并复制函数 _prepareCollection() 和 _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',
    ));  
于 2012-05-31T16:56:52.750 回答