Magento Admin Grids: Adding Columns
What I'm about to describe to you is very important, that is, if you plan to dabble in Magento admin grids. You could save hours or even days if you listen closely.
Let's start off by describing the scenario: Imagine that you are an admin of a Magento system. You are looking at your Sales->Order grid in the back-end and you realize that being able to filter this Sales->Order grid by Customer_Group could be useful.
The first thing you're going to want to do is open code/Mage/Adminhtml/Block/Sales/Order/Grid.php
For the sake of gaining some intuition, when you want to dabble in any back-end, admin functionality, there's a good chance the file(s) you want are in app/code/Mage/Adminhtml.
In Grid.php files you can find calls to a function called addColumn() inside of a function called _prepareColumns(). This is convenient, so we'll add our own.
You may be wondering about the renderer and the options. The 'renderer' decides how to display the customer group given its ID, and the 'options' populate the new customer_group dropdown on our grid. The Mage_Adminhtml_Block_Sales_Order_Renderer_CustomerGroup class hasn't been created yet, so lets do that now.
Create a new directory next to Grid.php called Renderer. Inside of this directory, create a file called, CustomerGroup.php. This new file should now be in code/Mage/Adminhtml/Block/Sales/Order/Renderer/CustomerGroup.php. The class will need two functions. One called renderer() and another called getCustomerGroupsArray()
At this point, you should be able to view your admin grid and see that the column has, indeed, been created. The dropdown should be populated, but you should not yet see any data in the grid for the customer_group column. This is because your addColumn() function depends on an index called group_id and the collection we are dealing with (from the sales_flat_order_item table) doesn't have access to a customer_group_id. See the _prepareCollection() function in Grid.php
Our goal, here in the _prepareCollection() function, is to take the collection of sales_flat_order_items and left-join the data to data from the customer_entity table which contains our customer_group information. In my situation, the _prepareCollection() function in Grid.php isn't actually called. Our project had some Amasty extensions that overrode the use of the local _prepareCollection() function, so I had to travel all the way over to app/code/local/Amasty/Orderattr/Block/Adminhtml/Order/Grid.php to find the correct _prepareCollection() function. Either way, find the correct _prepareCollection() function and modify it like this:
You should now see the populated customer group column on your admin page. Try to filter by a customer group. It should work. Now try to filter by the order#. If everything exploded, there is no need to fret. There's a simple fix for that problem.
You are probably seeing an error about ambiguous column names and such. To remove the ambiguity, all we need to do is go back to the _prepareColumns() function, find the columns that break when you filter, and add the following line of code to the addColumn() configuration array for each offending column:
'filter_index' => 'main_table.increment_id' // for the order#
Manually specifying the filter_index WITH the table name removes the ambiguity. Problem solved!