2013-04-28 5 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

Вы не можете получить имя клиента только в одном соединении кода строки. Имя и Фамилия - разные атрибуты, и вам нужно будет объединить их с вашей исходной коллекцией, а затем объединить их, чтобы они отображались как Полное имя.

Так в основном, Заменить

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

И заменить addColumn('name', код в _prepareColumns метод, при котором вы получаете имя клиента, с этим:

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

Спасибо за помощь! Я проверить свой код, то есть problem.if человек не авторизованы, вся информация будет не отображается в коде. –

+0

@JasonCheng Я сомневаюсь. Мы не получаем никакой пользы от сессии клиента, так что это не может быть проблемой. Также это для бэкэнд-сетки, поэтому логин клиента здесь не рассматривается. – Kalpesh

+0

Привет, kalpesh .. Я использовал выше код в моей настраиваемой сетке модуля, и он дал мне эту ошибку ---- Столбец не найден: 1054 Неизвестный столбец 'customer_name' в 'where clause', ..... Знаете ли вы, где я может быть неправильным. – shashank

0

Я вдохновляю свой ответ от Kalpesh , так что вы должны заменить этот код

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

с этими линиями

$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)')); 
Смежные вопросы