In the time that I’ve been working with Magento, one of the most powerful features I’ve come across is the rewrite feature. In Magento, rewriting allows developers to override and extend core Magento functionality. Below you can find examples of rewrites for blocks, models, and helpers; we’ll start out with blocks.
Creating a rewrite for a block is pretty straight forward. For this example, let’s say we want to modify the product view block. The first step would be to define the rewrite in the blocks tag of our config .xml:
<blocks> <catalog> <rewrite> <product_view>Pixafy_Foo_Block_Product_View</product_view> </rewrite> </catalog> </blocks>
What is going here is that we are telling Magento that the block Product_View is being rewritten to our custom block Pixafy_Foo_Block_Product_View. The next step is to create our new block. I prefer to keep the same structure of whatever it is that I am rewriting, so in this example, our block will be Foo/Block/Product/View.php.
Next, we declare our block’s class which extends from Mage_Catalog_Block_Product_View; doing this gives us access to the all the functions and variables defined in Mage_Catalog_Block_Product_View via inheritance.
From there, we are now free to override and extend 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>Pixafy_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>Pixafy_Foo_Helper_Invitation</data> </rewrite> </enterprise_invitation> </helpers>
–Rewriting the Invitation helper Data.php to our helper class which extends the rewritten class
This has been pretty handy for me. What development tricks do you use when developing for Magento? Share with me in the comments!