- Everytime magento release a new version, all core classes are replaced with completely new ones and so, custom code in core classes will be overriden
- Custom code is put inside our module files so it’s very helpful in sell module on Magento Connect
Now, lets get started
Magento Blocks
Assuming that we have to change or add a function in Mage_Catalog_Block_Product class, now we will override this class so that we don’t need to change the core file (core files should not be changed in any situation).
To override this class, open config.xml and put the following code inside the tags
1 2 3 4 5 6 7 | <blocks> <catalog> <rewrite> <product>Excellence_Test_Block_Catalog_Product</product> </rewrite> </catalog> </block> |
Next, we define a new class in the block folder of our custom module Mgtexplorer/new/Block/Catalog/Product.php
1 2 3 4 | <?php class <strong><em>Mgtexplorer</em></strong>_<strong><em>new</em></strong>_Block_Catalog_Product extends Mage_Catalog_Block_Product { } |
From now, whenever a catalog or product block’s object is created, Magento will make the object of our class, not the core class. Now we can make any changes here, for example: add new functions, change existing functions
1 2 3 4 5 6 7 8 9 10 11 12 | <?php class Mgtexplorer_new_Block_Catalog_Product extends Mage_Catalog_Block_Product { public function getPrice() { if(..some condition..){ return ..custom value..; }else{ return parent::getPrice(); } } } |
Magento Models
We can also override models. For example, we’re going to override Mage_Catalog_Model_Product class, open config.xml and insert the following code:
1 2 3 4 5 6 7 | <models> <catalog> <rewrite> <product>Mgtexplorer_Test_Model_Product</product> </rewrite> </catalog> </models> |
and our extended class would be
1 2 3 4 5 6 7 8 9 10 11 12 | <?php class Mgtexplorer_Test_Model_Product extends Mage_Catalog_Model_Product { public function getPrice() { if(..some condition..){ return ..some value..; }else{ return parent::getPrice(); } } } |
Magento Helpers
We can also override Helpers, in this tutorial we will override Mgtexplorer_Test_Helper_Data class, put the following code to config.xml file
1 2 3 4 5 6 7 | <helpers> <customer> <rewrite> <data>Mgtexplorer_Test_Helper_Data</data> </rewrite> </customer> </helpers> |
Similarly, define the custom Helper class in our custom module file.
1 2 3 4 | <?php class Mgtexplorer_Test_Helper_Data extends Mage_Customer_Helper_Data { } |
Note: we can only override classes which Magento creates objects for, there are many classes that cannot be overridden like abstract class Mage_Catalog_Block_Product_Abstract… In short, if a class is just extended but Magento doesn’t create objects for it => Overriding will not work.
Magento Controllers
Overriding a core controller is a little bit different:
- Overriding controller  is based on URL, not path of class
- The controller file which you extend needs to be required
1 2 3 4 5 6 7 8 | <global> <rewrite> <test_cart> <!--This can be any unique id --> <from><![CDATA[#^/checkout/cart/#]]></from> <!-- the URL to be overridden--> <to>/module/checkout_cart/</to> <!-- destination URL --> </test_cart> </rewrite> </global> |
Next, make a custom controller at Mgtexplorer/Test/controllers/Checkout/CartController.php
1 2 3 4 | <?php require_once 'Mage/Checkout/controllers/CartController.php'; class Mgtexplorer_Test_Checkout_CartController extends Mage_Checkout_CartController{ } |
My last words
Although these are very basic examples, they show you the basis of overriding the core blocks, models, helpers and controllers and you can base on my tutorial to make things more useful.
Feels free to drop comment/feedbacks, good luck with coding!