Add Total Number of Orders and Revenue Column to Customer Grid in Magento 1.9

customergridmagento-1.8magento-1.9

I want to add 2 columns to customer gird:
– Total number orders customer made.
– Total price of these orders.

Can anyone help me please ?

Best Answer

First,please follow @Amast post

Magento Customer Grid - Last Order Date

You can get Customer total order from this link...

But for getting total price .use below code

     $total_pricesql ='SELECT sum(o.base_grand_total)'
            . ' FROM ' . Mage::getSingleton('core/resource')->getTableName('sales/order') . ' AS o'
            . ' WHERE o.customer_id = e.entity_id ';
        $expr = new Zend_Db_Expr('(' . $total_pricesql . ')'); 

        $collection->getSelect()->from(null, array('total_price'=>$expr));
    $Totalsql ='SELECT COUNT(*)'
        . ' FROM ' . Mage::getSingleton('core/resource')->getTableName('sales/order') . ' AS o'
        . ' WHERE o.customer_id = e.entity_id ';
    $Totalsqlexpr = new Zend_Db_Expr('(' . $Totalsql . ')'); 

    $collection->getSelect()->from(null, array('orders_count'=>$Totalsqlexpr));




$this->addColumn('orders_count', array(
        'header'    => Mage::helper('customer')->__('Orders Count'),
        'index'     => 'orders_count',
    ));

            $this->addColumn('total_price', array(
                'header' => Mage::helper('sales')->__('Total),
                'index' => 'total_price,
                'type'  => 'currency',
                'currency' => 'base_currency_code',
            ));

Edit:

You custom renderer class .Please check http://inchoo.net/magento/how-to-add-custom-renderer-for-a-custom-column-in-magento-grid/ first.

From:

 $this->addColumn('total_price', array(
                'header' => Mage::helper('sales')->__('Total),
                'index' => 'total_price,
                'type'  => 'currency',
                'currency' => 'base_currency_code',
            ));

change:

$this->addColumn('total_price',
array(
'header'=> Mage::helper('total_price')->__('Order total price'),
'index' => 'total_price',
'renderer'  => 'Mage_Adminhtml_Block_Catalog_Product_Renderer_Red',// THIS IS WHAT THIS POST IS ALL ABOUT
));

}

Add

public function render(Varien_Object $row)
{
$value =  $row->getData($this->getColumn()->getIndex());
return '$'.$value.'</span>';

}
Related Topic