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

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

August 31, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to show Sum of Two fields in Grid in Magento Admin Panel

Showing the Sum of Two Fields in a Grid in Magento Admin Panel

As a senior developer working on an extension for Magento, you’ve encountered a common challenge: displaying the sum of multiple fields (in this case, ‘Stamp Cost’, ‘Ink Cost’, and ‘Form Cost’) in a single column within the admin data grid. Let’s walk through how to achieve this using Magento 2’s built-in grid functionality.

The Current Setup You’re currently adding a single field (‘stamp_cost’) to your grid:

$this->addColumn('stamp_cost', array(
    'header'    => Mage::helper('imprint')->__('Stamp Cost'),
    'width'     => '100px',
    'type'      => 'price',
    'currency_code' => $store->getBaseCurrency()->getCode(),
    'index'     => 'stamp_cost'
));

Displaying the Sum of Multiple Fields To show the sum of multiple fields, you’ll need to calculate the total in your collection and then access that value from your grid. Here’s a step-by-step guide:

  1. Update Your Collection

In your grid block class (usually named something like Mage_Adminhtml_Block_Sales_Order_Grid), update the _prepareCollection method to join all the required fields and calculate the total:

protected function _prepareCollection()
{
    $collection = Mage::getModel('imprint/imprint')->getCollection();
    $this->setCollection($collection);

    // Join Ink Cost and Form Cost tables
    $collection->join(array('ink' => 'imprint_ink_cost'),
        'main_table.id = ink.imprint_id',
        array('ink.stamp_cost', 'ink.ink_cost', 'ink.form_cost')
    );

    // Calculate total cost
    foreach ($collection as $item) {
        $totalCost = $item->getStampCost() + $item->getInkCost() + $item->getFormCost();
        $item->setTotalCost($totalCost);
    }

    return parent::_prepareCollection();
}
  1. Add the New Column to Your Grid

Next, add a new column for ‘Total Cost’ in your _prepareColumns method:

protected function _prepareColumns()
{
    // Other columns...

    $this->addColumn('total_cost', array(
        'header' => Mage::helper('imprint')->__('Total Cost'),
        'width'  => '100px',
        'type'   => 'price',
        'currency_code' => $store->getBaseCurrency()->getCode(),
        'index'  => 'total_cost'
    ));
}
  1. Update Your Grid PHTML File

Finally, ensure that the ‘Total Cost’ column is properly displayed in your grid.phtml file:

<column name="total_cost">
    <type>price</type>
    <currency_code><?php echo Mage::getDesign()->getActiveStore()->getBaseCurrencyCode() ?></currency_code>
    <header><translate>Total Cost</translate></header>
</column>

Conclusion

In this blog post, we’ve demonstrated how to display the sum of multiple fields (‘Stamp Cost’, ‘Ink Cost’, and ‘Form Cost’) in a single column (‘Total Cost’) within your Magento admin data grid. By updating your collection to join and calculate the total cost, adding a new column for ‘Total Cost’ in your grid block class, and ensuring proper rendering in your grid.phtml file, you can achieve this functionality with ease.

Remember that this is just one example of customizing the admin interface using Magento 2’s built-in grid system. To learn more about developing extensions and customizations for Magento 2, visit Laravel Company for comprehensive guides, tutorials, and resources tailored to Magento developers like you.

Happy coding!

Magento 2 Database Extensions Admin

Our network