In most cases the default Magento Product Grid under Catalog > Manage Products will display all the required information for easily filtering and searching the products, however in some particular cases store owners might want to be able to display and filter the catalog product grid by using non default attributes.
In this mini tutorial we will over a quick method for adding custom attributes to the backend product grid and displaying the option filters correctly. For this tutorial we will use the color and manufacturer attributes as examples.
After creating our base Module structure we need to rewrite the Adminhtml Catalog Product Grid in order for properly manipulate the collection and the grid columns:
Now that our rewrites are in place let’s copy of the Product Grid Block under our Module Block folder; the 2 functions that we will need to change are _prepareCollection and _prepareColumns.
_prepareCollection is in charge of setting up our collection and by default it only loads the necessary attribute for populating each column.
As we can see the addColumn function takes 2 parameters:
– A unique ID for the column, in this case our attribute code
– An array with the following values:
Header: The title for the column.
Width: The width value of the column in pixels
Type: The data type of the column(number, currency, options), this will take care of the formatting for our column values
Options: Allows to pre-populated the filter dropdown and is used to display correctly the values of our column
Index: is the matching attribute code from our collection
Now if we check our category grid in the Magento backend, we can see that the columns appear but they are empty this is because we haven’t told Magento where to get the option values for our columns yet.
So let’s go back to our Grid class and create the a new protected function: