Monday, March 21, 2011

Add new column on Customers Grid

We will start with this extension. For all your extensions we suggest: Never touch the core.

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
The collection provides us the original data and the Confirmation field of each customer. But as we can see, this field contains information like this 'e8e22a42f11011f9c9edd5cef96772de' or if the customer was confirmed, the value will be NULL. We need to create any function to evaluate this value and set on the field the right value: YES or NO.

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

Wednesday, March 16, 2011

Bienvenidos

Estamos muy felices de iniciar con este proyecto. La información que existe en la red acerca de Magento es muy interesante, pero pensamos en dudas más específicas.

Muchos muy buenos programadores están dándole a la Comunidad de Magento muchos amplios consejos y soporte, y nosotros también queremos contribuir con más información y ejemplos reales.

Nosotros estamos en México y pensando en que la gran parte de la información que existe en el internet esta en otros lenguajes, se irán dando estos tips para la comunidad en Español.

Nuestra intención no es otra más que crear un blog con soluciones reales a problemas del diario que se presentan entre los programadores Magento, así como crear una comunidad real de programadores Magento.

Si estas interesado en recibir la información que te interesó y más información en Inglés, déjanos un comentario y estaremos posteando el mismo caso en ese idioma. También te invitamos a seguirnos en Twitter @TipsMagento .
Follow Me!!!
Feliz Código!

TipsMagento Staff