Im trying to show the customers last login in the customer grid. (with data from the log_customer table)
To accomplish this, i created a Module with the neccesary Files.
With the help from these Links:
- Adding columns to customer grid using observer or overriding the customer grid
- Magento Customer Grid – Last Order Date
- magento add last login to customer grid
- Magento Grid – sortering and filtering works incorrectly after joining flat table to EAV
I see the empty row in the Customer grid, but all my attempts didnt brought me to the solution.
The Files
app\code\local\MyModule\ExtendCustomerGrid\Block\Adminhtml\Customer\Grid.php
class MyModule_ExtendCustomerGrid_Block_Adminhtml_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid {
protected function _prepareCollection() {
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('created_at')
->addAttributeToSelect('group_id')
->addAttributeToSelect('last_login')
->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');
// Retrieve data from log_customer
$sql = '(SELECT MAX(login_at)
FROM log_customer
WHERE log_customer.customer_id = e.entity_id
GROUP BY log_customer.customer_id)';
$expr = new Zend_Db_Expr('(' . $sql . ')');
$collection->getSelect()->columns(array('last_login' => new Zend_Db_Expr($sql)));
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns() {
...
$this->addColumn('last_login', array(
'header' => Mage::helper('customer')->__('Letzte Anmeldung'),
'width' => '80',
'type' => 'datetime',
'align' => 'center',
'index' => 'login_at',
'filter_index' => '`c_log`.`login_at`',
'gmtoffset' => true
));
...
$this->addExportType('*/*/exportCsv', Mage::helper('customer')->__('CSV'));
$this->addExportType('*/*/exportXml',
Mage::helper('customer')->__('Excel XML'));
return parent::_prepareColumns();
}
}
app\code\local\MyModule\ExtendCustomerGrid\etc\config.xml
<config>
<modules>
<MyModule_ExtendCustomerGrid>
<version>0.0.1</version>
</MyModule__ExtendCustomerGrid>
</modules>
<global>
<blocks>
<adminhtml>
<rewrite>
<customer_grid>MyModule__ExtendCustomerGrid_Block_Adminhtml_Customer_Grid</customer_grid><!-- rewrite the customer grid -->
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
app\etc\modules\MyModule_ExtendCustomerGrid.xml
<config>
<modules>
<MyModule_ExtendCustomerGrid>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Customer /><!-- module should depend on Mage_Customer -->
<Mage_Adminhtml /><!-- module should depend on Mage_Adminhtml also -->
</depends>
</MyModule_ExtendCustomerGrid>
</modules>
</config>
Thx in advance
Best Answer
Working
_prepareCollection
method:As you can see, only change is
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
instead of
return parent::_prepareCollection();