I have add a custom column in customer grid by copying core Grid.php file to local folder. I want to show last order date of customer in a column. So I have tried below code in collection.
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('created_at')
->addAttributeToSelect('group_id')
->addAttributeToSelect('track_device')
->addAttributeToSelect('inchoo_socialconnect_fid')
->addAttributeToSelect('inchoo_socialconnect_gid')
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
// add last_order_date column
$sql ='SELECT MAX(o.created_at)'
. ' FROM ' . Mage::getSingleton('core/resource')->getTableName('sales/order') . ' AS o'
. ' WHERE o.customer_id = e.entity_id ';
$expr = new Zend_Db_Expr('(' . $sql . ')');
$collection->getSelect()->from(null, array('last_order_date'=>$expr));
$this->setCollection($collection);
return parent::_prepareCollection();
}
here is the column code--
$this->addColumn('last_order_date', array(
'header' => Mage::helper('customer')->__('Last Order Date'),
'type' => 'datetime',
'align' => 'center',
'index' => 'last_order_date',
'filter' => true,
'sortable' => true ,
'gmtoffset' => true,
));
column showing correct values but Sorting and Filtering not working for this column. What is wrong there.