B-219 Sec-55 Noida, India

How to show Sum of Two fields in Grid in Magento Admin Panel

There are two ways to do it. Add the field to the collection and get the data from the database, or calculate it in PHP based on the 3 values returned from the DB. Doing the first way with the Magento Collection would, in my opinion, be too complex. instead, you want to use a Renderer (Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract)

First, inside of the Block/Adminhtml folder of your plugin, make a new folder called Renderer. Inside of it make a new file called CostSum.php with the following contents:

class Company_Module_Block_Adminhtml_Renderer_CostSum extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
    public function render(Varien_Object $row)
         return $row->getStampCost() + $row->getInkCost() + $row->getFormCost();

Then, in the grid, make a new column

$this->addColumn(‘cost_total’, array(
    ‘header’    => Mage::helper(‘imprint’)->__(‘Stamp Cost’),
    //’index’     => ‘Im not sure this is necessary’,
    ‘type’      => ‘price’,
    ‘currency_code’ => $store->getBaseCurrency()->getCode(),
    ‘renderer’ => new Company_Module_Block_Adminhtml_Renderer_CostSum()

(Visited 170 times, 1 visits today)

Leave a reply