You are here:

How to rewrite blocks in Magento?

Almost people who’ve been working with Magento are very impressive with rewrite feature, one of the most powerful features in this platform. In Magento, rewriting means that developers are allowed to override and extend core Magento functionality. In this post, we will show you the way to rewrite blocks in Magento.

It’s so simple to create a rewrite for a block in your Magento store.

First, you have to define the rewrite in the blocks tag of your config .xml:

<blocks>

<catalog>

                <rewrite>

                                <product_view>MageHit_Foo_Block_Product_View</product_view>

                </rewrite>

</catalog>

</blocks>

This step will help telling Magento that the block Products_view is being written to your custom block MageHit_Foo_Block_Product_View.

Next, you need to create your new block. We recommend you to keep the same structure. In this post, your block will be Foo/Block/Product/View.php.

After that, you declare your block’s class which extends from Mage_Catalog_Blog_Product_View, and doing this gives us access to the all the functions and variables defined in Mage_Catalog_Product_View via inheritance.

Thereby, we are now free to override and extends this block without modifying the core code.

The same process can be applied when rewriting models and helpers as seen below:

<models>           

<customer>                               

<rewrite>                                               

<customer>Magehit_Foo_Model_Customer</customer>                               

</rewrite>

</customer>

</models>

–Rewriting the Customer model to our model class which extends the rewritten class

<helpers>

<enterprise_invitation>                               

<rewrite>                                               

<data>MageHit_Foo_Helper_Invitation</data>                               

</rewrite>

</enterprise_invitation>

</helpers>

 

That’s easy, right? Creating a rewrite block is pretty straight forward and can’t difficult for you. If you have any problems, contact us for the help.

Thanks for reading!

>>>>> View more Magento extenisons & modules, Magento tips & tricks

Related articles
 

404

GET IT TOUTH

MageHit - No1 Magento Development Company

Back to Top