1

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

Can anyone help me please ?

1 Answer 1

3

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>';

}
7
  • I don't need to add a tab for each customer to show there number total orders. I need to show that info on customer grid page. Anyway, thanks you for your help :)
    – HungDQ
    Commented Jun 22, 2015 at 8:22
  • Great! It works perfectly, thanks you very much !. What should I do to add total number orders column to customer grid ?
    – HungDQ
    Commented Jun 22, 2015 at 8:40
  • question update ,please check...
    – Amit Bera
    Commented Jun 22, 2015 at 9:26
  • Great!, your code work like a charm. But I have 1 question: the currency is wrong format: 1280.0000. how can I show as: €1280.00. Thanks you very much.
    – HungDQ
    Commented Jun 22, 2015 at 9:37
  • not possible using this code... need something else
    – Amit Bera
    Commented Jun 22, 2015 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.