I'm adding a column to the order grid using observer approach:
- On the event ->
sales_order_grid_collection_load_before
I'm adding a join to the collection - On the event ->
core_block_abstract_prepare_layout_before
I'm adding a column to the grid
EDIT More Info:
On Event (1):
public function salesOrderGridCollectionLoadBefore($observer)
{
$collection = $observer->getOrderGridCollection();
$collection->addFilterToMap('store_id', 'main_table.store_id');
$select = $collection->getSelect();
$select->joinLeft(array('oe' => $collection->getTable('sales/order')), 'oe.entity_id=main_table.entity_id', array('oe.customer_group_id'));
}
On Event (2):
public function appendCustomColumn(Varien_Event_Observer $observer)
{
$block = $observer->getBlock();
if (!isset($block)) {
return $this;
}
if ($block->getType() == 'adminhtml/sales_order_grid') {
/* @var $block Mage_Adminhtml_Block_Customer_Grid */
$this->_addColumnToGrid($block);
}
}
protected function _addColumnToGrid($grid)
{
$groups = Mage::getResourceModel('customer/group_collection')
->addFieldToFilter('customer_group_id', array('gt' => 0))
->load()
->toOptionHash();
$groups[0] = 'Guest';
/* @var $block Mage_Adminhtml_Block_Customer_Grid */
$grid->addColumnAfter('customer_group_id', array(
'header' => Mage::helper('customer')->__('Customer Group'),
'index' => 'customer_group_id',
'filter_index' => 'oe.customer_group_id',
'type' => 'options',
'options' => $groups,
), 'shipping_name');
}
Everything work fine until I filter the grid with store view filter:
Column ‘store_id’ in where clause is ambiguous issue
I have printed the query:
SELECT `main_table`.*, `oe`.`customer_group_id`
FROM `sales_flat_order_grid` AS `main_table`
LEFT JOIN `sales_flat_order` AS `oe` ON oe.entity_id=main_table.entity_id
WHERE (store_id = '5') AND (oe.customer_group_id = '6')
As you case see store_id
miss main_table
alias.
To accomplish this I just need to set the filter_index
for store Id column but through the observer
So the question is how can I do it on the fly ?
Without overriding the block class ? ( otherwise the observer approach is useless )
Best Answer
Let's try this again with on other solution I mentioned before to you :-) , I have build the complete extension to show you how to add the field to the grid table. After that you only need a layout update file to add the column to you order grid page.
I called the extension Example_SalesGrid, but you can change it to your own needs.
Let's start by creating the module init xml in /app/etc/modules/Example_SalesGrid.xml:
Next we create our module config xml in /app/code/community/Example/SalesGrid/etc/config.xml:
Now we create the sql upgrade script in /app/code/community/Example/SalesGrid/sql/example_salesgrid_setup/install-0.1.0.php:
Next we create the layout update file in /app/design/adminhtml/default/default/layout/example/salesgrid.xml:
Now we need two Block files, one to create the filter options, /app/code/community/Example/SalesGrid/Block/Widget/Grid/Column/Customer/Group.php:
And the second to translate the row values to the correct text that will be displayed, /app/code/community/Example/SalesGrid/Block/Widget/Grid/Column/Renderer/Customer/Group.php:
The last file needed is only needed if you create an extra column from a table other than sales/order (sales_flat_order). All fields in sales/order_grid matching the column name from sales/order is automatically updated in the sales/order_grid table. If you need to add the payment option for example you will need this observer to add the field to the query so the data can be copied to the correct table. The observer used for this is in /app/code/community/Example/SalesGrid/Model/Observer.php:
This code is based on the example from
http://www.ecomdev.org/2010/07/27/adding-order-attribute-to-orders-grid-in-magento-1-4-1.htmlHope the example above solves your problem.