config.xml=========
<?xml version="1.0"?>
<config>
<modules>
<Quafzi_CustomerGridOrderCount>
<version>0.2.4</version>
</Quafzi_CustomerGridOrderCount>
</modules>
<global>
<models>
<quafzi_customergridordercount>
<class>Quafzi_CustomerGridOrderCount_Model</class>
</quafzi_customergridordercount>
</models>
</global>
<adminhtml>
<translate>
<modules>
<Quafzi_CustomerGridOrderCount>
<files>
<default>Quafzi_CustomerGridOrderCount.csv</default>
</files>
</Quafzi_CustomerGridOrderCount>
</modules>
</translate>
<events>
<core_layout_block_create_after>
<observers>
<quafzi_customergridordercount_adminhtml_block_html_before>
<class>quafzi_customergridordercount/observer</class>
<method>beforeBlockToHtml</method>
</quafzi_customergridordercount_adminhtml_block_html_before>
</observers>
</core_layout_block_create_after>
<eav_collection_abstract_load_before>
<observers>
<quafzi_customergridordercount_change_collection>
<class>quafzi_customergridordercount/observer</class>
<method>beforeCustomerCollectionLoad</method>
</quafzi_customergridordercount_change_collection>
</observers>
</eav_collection_abstract_load_before>
</events>
</adminhtml>
</config>
__________________________________________________________________________________
Observer.php=======
<?php
/**
* @package Quafzi_CustomerGridOrderCount
* @copyright Copyright (c) 2013 Thomas Birke
* @author Thomas Birke <tbirke@netextreme.de>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Quafzi_CustomerGridOrderCount_Model_Observer
{
public function beforeBlockToHtml(Varien_Event_Observer $observer) {
$block = $observer->getEvent()->getBlock();
if ($block instanceof Mage_Adminhtml_Block_Customer_Grid) {
$this->_modifyCustomerGrid($block);
}
}
protected function _modifyCustomerGrid(Mage_Adminhtml_Block_Customer_Grid $grid)
{
$this->_addOrderCountColumn($grid);
// reinitialisiert die Spaltensortierung
$grid->sortColumnsByOrder();
// reinitialisiert die Sortierung und Filter der Collection
$this->_callProtectedMethod($grid, '_prepareCollection');
}
/**
* dirty hack...
* @see http://www.webguys.de/magento/turchen-23-pimp-my-produktgrid/
*/
protected function _callProtectedMethod($object, $methodName)
{
$reflection = new ReflectionClass($object);
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invoke($object);
}
protected function _addOrderCountColumn($grid)
{
$grid->addColumnAfter('order_count', array(
'header' => Mage::helper('customer')->__('Order Count'),
'align' => 'center',
'width' => '80px',
'type' => 'number',
'filter' => false,
'index' => 'order_count'
), 'customer_since');
}
public function beforeCustomerCollectionLoad(Varien_Event_Observer $observer)
{
$collection = $observer->getEvent()->getCollection();
if ($collection instanceof Mage_Customer_Model_Resource_Customer_Collection) {
$relationAlias = 'orders_to_count';
$from = $collection->getSelect()->getPart(Zend_Db_Select::FROM);
if (false === array_key_exists($relationAlias, $from)) {
// not yet joined
$orderTableName = Mage::getSingleton('core/resource')
->getTableName('sales/order');
$collection
->getSelect()
->joinLeft(
array($relationAlias => $orderTableName),
$relationAlias . '.customer_id=e.entity_id',
array('order_count' => 'COUNT(' . $relationAlias . '.customer_id)')
);
$collection->groupByAttribute('entity_id');
}
}
}
}
Month: November 2014
Add Customer Order Count in Order Grid
$collection->join(‘order’, ‘main_table.entity_id = order.entity_id’, ‘customer_email’);
$alias = ‘subselect’;
$subselect = Mage::getModel(‘Varien_Db_Select’,
Mage::getSingleton(‘core/resource’)->getConnection(‘core_read’)
)->from(‘sales_flat_order’, array(
‘customer_email as s_customer_email’,
‘count(*) as total_orders’)
)->group(‘customer_email’);$collection->getSelect()
->joinInner(array($alias => $subselect),
“{$alias}.s_customer_email = order.customer_email”);_____________________________________________________________________________________
$this->addColumn(‘total_revenue’, array(
‘header’ => Mage::helper(‘sales’)->__(‘Total Revenue’),
‘index’ => ‘total_revenue’,
‘filter_index’ => ‘ctotals.total_revenue’,
));$this->addColumn(‘total_orders’, array(
‘header’ => Mage::helper(‘sales’)->__(‘Total Orders’),
‘index’ => ‘total_orders’,
‘filter_index’ => ‘ctotals.total_orders’,
));
Add Customer Email in Order Grid
$collection->join(‘order’, ‘main_table.entity_id = order.entity_id’, ‘customer_email’);
_____________________________________________________________________________________
$this->addColumn(‘customer_email’, array(
‘header’ => Mage::helper(‘sales’)->__(‘Customer Email’),
‘index’ => ‘customer_email’,
));