:::: MENU ::::

Magento certification: Create and add code to pages

Magento certification: Create and add code to pages
4 votos, 4.25 media (84% puntos)

How can code be modified or added to Magento pages using the following methods?

Magento is known by its great flexibility to add custom code or modifying the existing one. For that you can explore multiple possibilities. The next sections shows some of them, but there are much more you can explore by yourself.

Template customizations

First and simplest method to add some custom code to your Magento shop, is to modify some of the template files of your active theme.

This kind of modifications are used mainly to modify the markup of a page or block, adding or modifying the way the data is shown and other similar ‘simple’ things.

If it’s the first time you modify a particular template file, it should be located under the following folder:


The recomended way of modifying a template file, is to copy it to your theme folder and doing the modifications over the duplicated file. This way you preserve the Magento core code, and it would be easier to upgrade your Magento install.

Layout customizations

By modifying layout files you can achieve some of the following:

    • Moving a block from one area to other.
    • Adding a new block to a particular area or block.

Also by means of the layout files you may set data to block instances in order to modify their behaviour. A typical example of that is to set the number of items to be shown on a list, or the number of columns a block will render their data.

Overriding block classes

You can override a block class in two different ways:

By copying the block definition file to your local code pool

This is the most simple way of overriding but it is not the best way of doing it. You need to copy the class file to your local code pool, keeping the rest of the path as it is. For example, if you want to override some behaviour of the cart block, you will need to copy the file:


To your local code pool:


By overriding through a Magento layout file

This way is more appropriate as it keeps the code cleaner and it allows an easier upgrade of Magento platform. For doing that, you will need to define a rewrite of the block through a Magento layout file. This is an example:


This fragment defines a rewrite for the block Mage_Catalog_Product_View.

Then in your code, you will need to define you block as inheriting from the original class:

  class Namespace_Module_Block_Blockname extends Mage_Catalog_Block_Product_View { }

Please, do not forget to include a dependency to the Catalog module in your module definition file, as it will be needed that the Catalog module loads first.

Registering observers in Magento

To register an observer, you’ll need to add a code like the following in your module config.xml file:


This fragment will register a new event observer that will be executed when any product in the catalog is saved in the current store.

You need to provide an entry point to a class and a method, and then you will need to define in your class, like de following:

class Namespace_Module_Model_Observer {
   * Al guardar un producto agrupado, tenemos que guardar su
   * URL en la redirección de los productos simples 
  public function hookMethodName($observer) {

Hey! Qué opinas sobre el artículo?