Magento : add two column value in collection

August 30, 2010 | In: Magento, mysql, php, web development

many times we need to do operation on two column values and show in the admin panel. for this we have to follow these steps:
1. Add column in your Grid.php file:

<?php 
$this->addColumn('newcolumn', array(
          'header'    = Mage::helper('core')->__('NewColumn'),
          'align'       = 'left',
          'index'      = 'created_at',
          'type'       = 'action',
          'renderer' = new Namespace_Module_Block_Adminhtml_Renderer_Qty(),
      ));
?>

2. Now create a folder in the AdminHtml folder with name ‘Renderer’
3. Create a new php file with name ‘Qty.php’
4. Write below code in the Qty.php file:

<?php 
class Namespace_Module_Block_Adminhtml_Renderer_Qty extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
       // Here you will get complete row value and now you can do operation on row field value 
       // For example here I want to show remaining value.
      $remainingvalue = $row->getQty() - $row->getMinQty();
      return $remainingvalue;
    }
}
?>

Ref: http://www.magentocommerce.com/boards/viewthread/192232/#t239222
That’s all.