2013-04-28 50 views
2

我想添加一个新的column对位于这里的销售订单网格客户名称:的Magento - Magento中添加客户名称,以便电网1.7.0.2

App/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php 

我要添加客户名称类似名称在管理客户。

我已经添加了下面的代码:

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', 
    )); 
} 

客户电子邮件是确定的,但增加了客户名称不工作了!

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

回答

14

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

所以基本上,替换

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

与此代码

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

而且在_prepareColumns方法取代你addColumn('name',代码,你所得到的用户名,与此:

$this->addColumn('customer_name', array(
     'header' => Mage::helper('sales')->__('Customer Name'), 
     'index' => 'customer_name', 
     'filter_name' => 'customer_name' 
    )); 
+0

谢谢大家帮忙将这段代码

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

!我测试你的代码,然后有一个问题。如果对方没有登录,所有的信息将不会显示在您的代码中。 – 2013-04-28 14:51:07

+0

@JasonCheng我对此表示怀疑。我们没有从客户会话中获得任何价值,所以这不是问题。这也适用于后端网格,因此客户的登录不在这里考虑。 – Kalpesh 2013-04-29 09:47:46

+0

嗨kalpesh ..我在我的自定义模块网格中使用上面的代码,它给了我这个错误----列未找到:1054未知列'客户名'在'where子句',.....你知道我在哪里可能是错的。 – shashank 2016-02-09 05:18:49

0

我从Kalpesh启发我的回答 所以你应该与这些线

$customerTable = Mage::getResourceSingleton('customer/customer')->getEntityTable(); 
$firstnameAttribute=Mage::getResourceSingleton('customer/customer')->getAttribute('firstname'); 

$firstnameAttributeTable = $firstnameAttribute->getBackend()->getTable(); 

$lastnameAttribute=Mage::getResourceSingleton('customer/customer')->getAttribute('lastname'); 
$lastnameAttributeTable = $lastnameAttribute->getBackend()->getTable(); 
$collection->getSelect() 
->join(array('customer_varchar1'=>$firstnameAttributeTable),'main_table.customer_id =customer_varchar1.entity_id and customer_varchar1.attribute_id='.$firstnameAttribute->getId(), 
     array('customer_varchar1.value')) 
    ->join(array('customer_varchar2'=>$lastnameAttributeTable),'main_table.customer_id =customer_varchar2.entity_id and customer_varchar2.attribute_id='.$lastnameAttribute->getId(), 
     array('customer name'=>'CONCAT(customer_varchar2.value ," " ,customer_varchar1.value)'));