Magento 1.8 Orders – Add Customer Email to Adminhtml Order Grid

adminhtmlcoreordersoverrides

I am trying to add the Customer Email to our adminhtml order grid. We are overriding the code in Mage/core in this file: app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php

In PrepareColumns we add

$this->addColumn('customer_email', array(
    'header'    => Mage::helper('Sales')->__('Email'),
    'width'     => '100px',
    'index'     => 'customer_email',
    'type'        => 'text',

));

And in _prepareCollection we added this

$customer_entity = Mage::getSingleton('core/resource')->getTableName('customer_entity');
        $collection->getSelect()
               ->join(
               $customer_entity,
               'main_table.customer_id = '.$customer_entity.'.entity_id', array('customer_email' => 'email'));

The good news is, that it does add the email column

My question: Now the only problem is that when we enter an order id, or sku as filter that the result is the Magento error page …. So something is still going wrong here

What are we forgetting? Or what is the best code for Magento 1.8 to add the Email address in the order grid?

Best Answer

Just copy Grid.php to app/code/local/Mage/Adminhtml/Block/Sales/Order/ Then append sales_flat_order table to collection

 protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass());
        $collection->getSelect()->join('sales_flat_order', 'main_table.entity_id = sales_flat_order.entity_id',array('customer_email'));
        $this->setCollection($collection);
        return parent::_prepareCollection();
    };

And a addColumn to _prepareColumns()

 $this->addColumn('customer_email', array(
    'header' => Mage::helper('sales')->__('Payment method'),
    'index' => 'customer_email',
    'filter_index' => 'sales_flat_order.customer_email',
    ));

More details can be find here.

Related Topic