magento - 在magento 1.7.0.2中添加客户名称以订购网格

Jas*_*eng 2 magento magento-1.7

我正在尝试column在此处的销售订单网格中添加一个新的客户名称:

App/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php
Run Code Online (Sandbox Code Playgroud)

我想在管理客户中添加名称,如姓名.

我添加了以下代码:

protected function _getCollectionClass()
{
    return 'sales/order_grid_collection';
}

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    /*junpeng add start*/

    $collection->getSelect()
    ->join(
    'customer_entity',
    'main_table.customer_id = customer_entity.entity_id', array('email' => 'email'));

    $collection->getSelect()->join(
    'customer_entity_varchar',
    'main_table.entity_id = customer_entity_varchar.entity_id', array('name' => 'value')
    );

    /*junpeng add end*/
    $this->setCollection($collection);
    return parent::_prepareCollection();
}
protected function _prepareColumns()
{
    $this->addColumn('name', array(
        'header'    => Mage::helper('sales')->__('Customer Name'),
        'index' => 'name',
    ));

    $this->addColumn('email', array(
        'header'    => Mage::helper('Sales')->__('Customer Email'),
        'index'     => 'email',
        'type'        => 'text',
    ));
}
Run Code Online (Sandbox Code Playgroud)

客户电子邮件是可以的,但添加客户名称不起作用!

有人可以帮我解决这个问题吗?

Kal*_*esh 14

您只能通过一行代码连接获取客户名称.Firstname和Lastname是不同的属性,您需要将它们与原始集合连接起来,然后将它们连接起来以显示为Fullname.

所以基本上,替换

$collection->getSelect()->join(
    'customer_entity_varchar',
    'main_table.entity_id = customer_entity_varchar.entity_id', array('name' => 'value')
    );
Run Code Online (Sandbox Code Playgroud)

用这个代码

$fn = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'firstname');
$ln = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'lastname');
$collection->getSelect()
    ->join(array('ce1' => 'customer_entity_varchar'), 'ce1.entity_id=main_table.customer_id', array('firstname' => 'value'))
    ->where('ce1.attribute_id='.$fn->getAttributeId()) 
    ->join(array('ce2' => 'customer_entity_varchar'), 'ce2.entity_id=main_table.customer_id', array('lastname' => 'value'))
    ->where('ce2.attribute_id='.$ln->getAttributeId()) 
    ->columns(new Zend_Db_Expr("CONCAT(`ce1`.`value`, ' ',`ce2`.`value`) AS customer_name"));
Run Code Online (Sandbox Code Playgroud)

并使用以下方法替换您获取客户名称的方法中的addColumn('name',代码_prepareColumns:

$this->addColumn('customer_name', array(
        'header'    => Mage::helper('sales')->__('Customer Name'),
        'index' => 'customer_name',
        'filter_name' => 'customer_name'
    ));
Run Code Online (Sandbox Code Playgroud)