2013-04-28 48 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)'));