Magento Developper’s Guide (Lesson 8) – Rewrite / modify a magento block

14 novembre 2011

This tutorial is the 8th of many , if it’s not already done, i suggest you read to the tutorials starting with the summary of this series.

Sometimes you need to change the behavior of Magento, we want for exemple to change the behavior of a block to display information about a product.

In this tutorial we will see how to change a basic block of Magento. When you want to do this kind of modification, it is better to keep in mind one of the basic rules magento development (attention if I make an exam … it will be asked..so remember ! XD)

« We never modify the files located in the core »

NEVER…NEVER…NEVER…it’s very important remember forget the idea of ​​going directly edit the files in/app/code/core.

Instead, we will overload this Block, create a class that will extend this core Block and rewrite only the part you want to change. Then, in order to use it, declare your file in our config.xml file (as usual).

1 – Start by re-creating a module in your folder Pfay and call it MyProduct

So create the file:

/app/code/local/Pfay/MyProduct

With etc/config.xml and do not forget to declare the Pfay_All.xml (see lesson 2 ).

2 – Create your « modified » Block

What interests us is to rewrite the block Mage_Catalog_Block_Product_View. Let’s say we want to rewrite the method « showInfos » of this Block.

So we will create in /app/code/local/Pfay/MyProduct/Block/Product/file view.php.

Which will contain:

Class Pfay_MonProduit_Block_Product_View extends Mage_Catalog_Block_Product_View
{
   public function afficherLesInfos()
   {
      return 'les infos complementaires';
   }
}

3 – Tell Magento to take into account the new version of this block

Edit the config.xml of your module MyProduct and put in it:

<?xml version="1.0"?>
<config>
 <modules>
    <Pfay_MonProduit>
        <version>1.0.0</version>
    </Pfay_MonProduit>
 </modules>
 <global>
   <blocks>
    <catalog>
         <rewrite>
              <product_view>
                    Pfay_MonProduit_Block_Catalog_Product_View
               </product_view>
             </rewrite>
      </catalog>
  </blocks>
 </global>
</config>

With these tags, we say we will configure a block of Magento’s core called « catalog » and we will rewrite () the « product_view » block of this module.

Now it will take our new block to replace the base.

Conclusion:

« Et voila ! » (the french for « Here we are » ;) ) You now know to rewrite a block;)

As usual, feel free to leave your messages and make a comment links to these tutorials on your websites. It’s always nice:)

Go to the summary of this tutorial

You should read this too :
  • No Related Post
Charla # 6 décembre, 2011

That saves me. Thanks for being so snesible!

# 19 décembre, 2012 stroom

Thank you for your great tutorials. I learned a lot.
Tis tutorial is not working for me so I googled a bit and found some information:

config.xml should be:

1.0.0

Pfay_MyProduct_Block

Pfay_MyProduct_Block_Catalog_Product_View

and the file View.php should be in:
/app/code/local/Pfay/MyProduct/Block/Catalog/Product/

Thanks again for your great work.

stroom # 19 décembre, 2012

Ah, I forgot:
If you want to test your view, try to overwrite an existing function like:

public function canEmailToFriend()
{
return false;
}

If the code is working, the email to friends links disappear.

# 21 décembre, 2012 Ome Ko

Pfay_MonProduit_Block_Catalog_Product_View

throws a Fatal error: Call to a member function addToChildGroup() on a non-object

no spaces/new lines allowed here in your config file!

Pierre FAY # 24 décembre, 2012

Thanks Stroom :) have you finded the problem ? :) sorry for the time of response, i’m currently very very busy at work :)

# 13 juillet, 2013 aman verma

@Ome Ko

remove the spaces in config.xml

Pfay_MonProduit_Block_Catalog_Product_View

should be

Pfay_MonProduit_Block_Catalog_Product_View

ilkerb # 29 novembre, 2013

To make this work, I had to made some changes.

Pfay_MonProduit_Block_Catalog_Product_View

Clear all spaces in config.xml to make it look like above.

Move View.php to Block/Catalog/Product and change its class declaration too (add Catalog between block and catalog)

By the way Pierre, this is my first comment in your whole series. I would like to thank you for doing this kind of great work. Thanks a lot.

# 1 janvier, 2014 Chathura janaka

This should be like this.no spaces.it’s work for me.
thanks Pierre FAY =) .

Pfay_MonProduit_Block_Catalog_Product_View

Carmelo # 3 janvier, 2014

i’ve solved, thanks,
it was an error in my configuration

# 22 janvier, 2014 Innam Hunzai

Working Fine… Thanks Man! really appreciated your work….

Regards,
Innam Hunzai
Developer

Hashid Hameed # 18 février, 2014

I got addChild fatal error. I renamed my module name from HashProduct to MyProduct and it worked. No idea why. Maybe Hashproduct was too long ?

# 24 février, 2014 Hashid Hameed

View.php should be in Namespace/Module/Block/Product/View.php
And in config.xml
Namespace_Module_Block_Product_View

Hashid Hameed # 24 février, 2014

View.php should be in Namespace/Module/Block/Product/View.php
And in config.xml

Namespace_Module_Block_Product_View

Vous aussi donnez votre avis
(requis)



Expert magento sur Lille - Pierre FAY