Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

add column whose value is calculated by php method to grid
 
apl
Member
 
Total Posts:  33
Joined:  2008-12-16
 

I want to add a column to order grid. The value in this column is not field value from database, but a value return from php method. For example, I have a method which takes order date as parameter and return another special date. The returned date is based on order date but varies between various scenarios (so I am not able to get it calculated using mysql functions). I do not want to save this date into database.

I have looked at Mage_Adminhtml_Block_Sales_Order_Grid and add some code to _prepareCollection()

foreach ($collection as $order{
    $order
->setData('mycolumn'MyHelperClass::calculateSpecialDate($order->getCreatedAt()));
}

Now I can see the column added into the grid, but I can not operate on this column to sort or to filter as other columns do. How can I make this column fully functional in grid as if it was one of order attributes? But I really do not like to save it as attribute before I can use it in grid. I run into many cases require extra column values built on the fly. I just use order grid as an example to explain what I want to achieve.

Many thanks if you can contribute your ideas!

 
Magento Community Magento Community
Magento Community
Magento Community
 
OggettoWeb
Jr. Member
 
Avatar
Total Posts:  6
Joined:  2010-03-02
 

Do next:

1. Add column in your Grid.php file or rewrited file in next way

$this->addColumn('mycolumn', array(
          
'header'    Mage::helper('core')->__('My Column'),
          
'align'     ='left',
          
'index'     'created_at',
          
'type'    ='action',
          
'renderer' = new Namespace_Module_Block_Adminhtml_Renderer_Date(),
      ));

2. Create class Namespace_Module_Block_Adminhtml_Renderer_Date
here is the code

<?php

class Namespace_Module_Block_Adminhtml_Renderer_Date extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    
public function render(Varien_Object $row)
    
{
         $data 
MyHelperClass::calculateSpecialDate($row->getData($this->getColumn()->getIndex()));
          return 
$data;
    
}
}

?>

it seems to me - that’s all

 
Magento Community Magento Community
Magento Community
Magento Community
 
apl
Member
 
Total Posts:  33
Joined:  2008-12-16
 

You are a star!

Thank you!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Dieter Gribnitz
Jr. Member
 
Total Posts:  4
Joined:  2008-10-06
 

Wow! Thanks a lot.
I can’t even begin to explain how long I was searching for a solution to a similar issue.
This bit of knowledge gives me a lot of power! Ha ha ha!!!
If anyone knows of a good source of documentation to admin form functionality, Pleeeaaase let me know.
Thanks again for sharing.

 
Magento Community Magento Community
Magento Community
Magento Community
 
apl
Member
 
Total Posts:  33
Joined:  2008-12-16
 

nowhere. analysing the code is the source

 
Magento Community Magento Community
Magento Community
Magento Community
 
MagePsycho
Moderator
 
Avatar
Total Posts:  1702
Joined:  2009-06-23
 
oggetto web - 24 May 2010 03:42 AM

Do next:

1. Add column in your Grid.php file or rewrited file in next way

$this->addColumn('mycolumn', array(
          
'header'    Mage::helper('core')->__('My Column'),
          
'align'     ='left',
          
'index'     'created_at',
          
'type'    ='action',
          
'renderer' = new Namespace_Module_Block_Adminhtml_Renderer_Date(),
      ));


2. Create class Namespace_Module_Block_Adminhtml_Renderer_Date
here is the code

<?php

class Namespace_Module_Block_Adminhtml_Renderer_Date extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    
public function render(Varien_Object $row)
    
{
         $data 
MyHelperClass::calculateSpecialDate($row->getData($this->getColumn()->getIndex()));
          return 
$data;
    
}
}

?>

it seems to me - that’s all

Really great post. I am gonna bookmark this thread.
Cheers

 
Magento Community Magento Community
Magento Community
Magento Community
 
ninjured
Jr. Member
 
Total Posts:  24
Joined:  2010-06-21
 

where do i put the code for the new class that is created ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Hemendra Singh
Jr. Member
 
Total Posts:  1
Joined:  2010-06-22
 

The class name is - Namespace_Module_Block_Adminhtml_Renderer_Date

So the file path would be :

Namespace / Module / Block / Adminhtml / Renderer / Date.php

Finally, include this is file at the start of Grid.php

Hope this might help.....smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
ronniee
Jr. Member
 
Avatar
Total Posts:  3
Joined:  2009-10-27
Dhaka, Bangladesh
 

WOW! what a concept it is!  gulp

Thank you very much, it worked for me.

 
Magento Community Magento Community
Magento Community
Magento Community
 
lfolco
Jr. Member
 
Avatar
Total Posts:  21
Joined:  2010-05-22
 

has anyone gotten this to work with the sort and the filtering though?

my situation is slightly different; my derived column is based on a lookup: i am looking up the value for an id in the eav_attribute_option_value table:

public function lookupColorValue($option_id{
  
    
//SELECT VALUE FROM eav_attribute_option_value WHERE option_id = 98;
  
    
$read Mage::getSingleton('core/resource')->getConnection('core_read');
    
$select $read->select();
    
$select->from('eav_attribute_option_value''value')
           ->
where('option_id = ?'$option_id);
    
$value $read->fetchOne($select);  
    return 
$value;
  
}

i’m using this method in a helper and setting up the column like this:

$this->addColumn('color', array(
      
'header'    => Mage::helper('colorcodes')->__('Color'),
      
'align'     => 'left',
      
'width'     => '150px',
      
'index'     => 'option_id',
      
'type'      => 'action',
      
'renderer'  => new Namespace_Colorcodes_Block_Adminhtml_Renderer_Colorvalue(),
    ));

it displays correctly, but i cannot filter or sort by the text value; the column behaves the same as the option ID column next to it (which is displayed as is).

 
Magento Community Magento Community
Magento Community
Magento Community
 
CoastAMS
Member
 
Avatar
Total Posts:  57
Joined:  2011-03-24
Bath
 

How would I be able to add a “Last Order Date” column to a Customer list export?

Thanks

Paul

 
Magento Community Magento Community
Magento Community
Magento Community
 
fabersys
Jr. Member
 
Avatar
Total Posts:  13
Joined:  2009-06-25
Hyderabad
 

Hey All,

I have created a custom lifetime sales column in Magento admin customer grid. Please see my attached Grid.php code in \public_htmlapp\code\core\Mage\Adminhtml\Block\Customer\Gid.php

Please see the attached screen.

I have created a custom lifetimes sales column using a renderer which gives the lifetime time sales of each customer. I need filters like From: To: and Ascending and Descending.

Can anyone please help me in this regard?

Image Attachments
customer-grid.png
File Attachments
Grid.php  (File Size: 10KB - Downloads: 197)
 
Magento Community Magento Community
Magento Community
Magento Community
 
FetchDesigns
Sr. Member
 
Avatar
Total Posts:  124
Joined:  2008-04-11
Chicago, IL
 

Anyone successfully get the sort and filter working on these custom columns?

 
Magento Community Magento Community
Magento Community
Magento Community
 
PatrickTT
Jr. Member
 
Total Posts:  22
Joined:  2010-10-14
 

Is it possible to add a column which has a calculation? I would like to add a column which shows the GST component of a price, so I don’t need to use an existing field, just calculate 10% of the price and display that.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top