Ok, the requirement is simple: "I need a new column on my Customers Grid (Admin) showing if customer is Confirmed or not".
Ok, we'll start with the creation of our enviroment:
Step 1. Building the extension tree files.
You will need to create your files on your Local scope.Tree Folder Structure |
Step 2. Creating Configuration.
The structure of the etc/config.xml it's very simple. We just need to set the rewrite of Grid Class and set our new Class.
Step 3. Getting the Collection.
We will blog later other way to get the data for this column, but right now the option will be Setting the Collection using the setCollection method. In this case (our boss, customer, company, girlfriend, etc.) requires to know if the customers on list are Confirmed or not on new column.You can see the original code in app/code/core/Mage/Adminhtml/Block/Customer/Grid.php and you will see the protected method _prepareCollection():
This query just have the required information for the original grid (name, email, creation date, group, postcode, city, telephone, region and country) and if you prints the SQL and executes, you'll see the list of the values and our Confirmation field isn't present.
So, what can we do?
Ok, the nature of Magento Dataflow is very strict: if you overrides this method, you'll need to set the collection to parent and the parent will re-structure the query and our query will be ignored. Then, we need to override the next step after the query: method setCollection($collection).
This method will receive the collection and will set the collection variable with the result of the query (values inside the collection). So, we just need to set our new attribute to select like the next code (remember, we can use the method addAttributeToSelect because the customer collection was created under Mage_Eav_Model_Entity_Collection. If you need to get any data from other model, you will need to study the Use of Collections in Magentocommerce Wiki):
Step 4. Setting the new Column on our grid.
Ok, we have all the data in our collection and now it's time to set our column in grid. Now we'll complete our class with the protected method _prepareColumns(). We have two options to do this: the first one is just creating the same method BUT you'll need to call before your setting to parent method _prepareColumns() and returning the same parent method:This option will create the column at the end of our grid. If we want to set this column in any specific order, we can copy/paste the original method and just setting the new column.
Ok, now we can see our class complete:
Ok, that's it!. Now we need to register our extension in Magento setting the Company_Adminhtml.xml on app/etc/modules/ and after this, we must clean the cache.
And this will be the result:
Customers Grid with new column |
This is the reason for the next file Block/Render/Confirmation.php. We will extend the Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract class and we will set the render method in our class:
After this, we must to set the render in our Grid Override and we will get the information exactly as we want:
And, this is the final result:
Customers Grid with new Column and correct value |
And, we finally finish!. We will hope this can be helpful for the community.
Thanks.
TipsMagento Staff