【问题标题】:magento - add customer name to order grid in magento 1.7.0.2magento - 在 magento 1.7.0.2 中将客户名称添加到订单网格
【发布时间】:2013-04-28 01:38:06
【问题描述】:

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

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

客户邮箱可以,但是添加客户名不行!

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

【问题讨论】:

    标签: magento magento-1.7


    【解决方案1】:

    您无法通过一行代码加入来获取客户名称。 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'
        ));
    

    【讨论】:

    • 感谢您的帮助!我测试了您的代码,然后出现了问题。如果此人未登录,则所有信息都不会显示在您的代码中。
    • @JasonCheng 我对此表示怀疑。我们没有从客户的会话中获得任何价值,所以这不是问题。这也是针对后端网格的,所以这里不考虑客户的登录。
    • 嗨 kalpesh ..我在自定义模块网格中使用了上面的代码,它给了我这个错误----找不到列:1054 'where 子句'中的未知列'customer_name',... ..你知道我哪里错了吗。
    【解决方案2】:

    有一个免费的 magento 扩展:

    http://www.magentocommerce.com/magento-connect/enhanced-admin-grids-editor.html

    它目前是 alpha,但我已经对其进行了测试,它在 1.6 和 1.7 上都能完美运行!

    【讨论】:

      【解决方案3】:

      我从 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)'));
      

      【讨论】:

        猜你喜欢
        • 2013-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多