"We" (another group) implemented an extension on a Magento Hackathon which allows to easily add new columns, rearrange them and remove columns from the grid. Furthermore you can join tables to the collection to add columns.
How to add new column to order grid using observers?
-
07-12-2021 - |
Question
Could you help me? I'm trying to add new column to the order grid in admin using observers. there is my config.xml
<adminhtml>
<events>
<adminhtml_block_html_before>
<observers>
<order_grid>
<type>model</type>
<class>Order_Grid_Model_Observer</class>
<method>addItemsColumn</method>
</order_grid>
</observers>
</adminhtml_block_html_before>
</events>
</adminhtml>
There is my observer code:
class Order_Grid_Model_Observer
{
public function addItemsColumn($observer)
{
$_block = $observer->getBlock();
$_type = $_block->getType();
if ($_type == 'adminhtml/sales_order_grid') {
$_block->addColumn('total_item_count', array(
'header'=> Mage::helper('sales')->__('Items'),
'width' => '80px',
'type' => 'text',
'index' => 'total_item_count',
'sortable' => false,
'filter' => false
));
$_block->getColumn('real_order_id')
->setData('filter_index', 'main_table.increment_id');
$collection = $_block->getCollection();
$collection->clear();
$collection->getSelect()
->joinLeft(array('o' => 'sales_flat_order'), 'o.entity_id = main_table.entity_id', array('total_item_count'));
$_block->setCollection($collection);
}
}
}
And I almost did it, but when I try to sort grid by some field, I get the error "Column 'increment_id' in field list is ambiguous". It's weird because I've updated 'filter_index' for 'increment_id' field. Any ideas why collection for this block was not updated? Thanks.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow