This is an integral part of magento module development.
This is required because
- As we know when ever magento is updated all core classes are replaced with new class. So if we put our custom code in core classes, it will get overwritten
- All our custom code is organized inside our module file, this help us in deploying the module in magento connect as well
Lets see how overriding is done
Blocks
Suppose we have a requirement to make some changes or add a function in Mage_Catalog_Block_Product class. Since this a core file, what we will do is override this class so that there is no need to change the core file. To do this, in config.xml inside the tags. We write
HTML Code:
<blocks>
<catalog>
<rewrite>
<product>Excellence_Test_Block_Catalog_Product</product>
</rewrite>
</catalog>
</block>
We create a new class in our block folder at path Excellence/Test/Block/Catalog/Product.php (this is in our custom module)
PHP Code:
<?php
class Excellence_Test_Block_Catalog_Product extends Mage_Catalog_Block_Product
{
}
So this way now, when ever catalog/product block’s object is created inside magento, magento will create object of our class instead of the core class. No we can add functions here or update existing functions.
PHP Code:
<?php
class Excellence_Test_Block_Catalog_Product extends Mage_Catalog_Block_Product
{
public function getPrice()
{
if(..some condition..){
return ..custom value..;
}else{
return parent::getPrice();
}
}
}
Right now if you notice, our class has extended Mage_Catalog_Block_Product. It is not necessary to do this, but the advantage is that all other functions of the parent class are automatically inherited to our class. So we don’t need to define all functions.
Models
Similar to block, we can override models. Support we want to override the Product classes, i.e Mage_Catalog_Model_Product class. In config.xml we need to write
HTML Code:
<models>
<catalog>
<rewrite>
<product>Excellence_Test_Model_Product</product>
</rewrite>
</catalog>
</models>
and our class would be
PHP Code:
<?php
class Excellence_Test_Model_Product extends Mage_Catalog_Model_Product
{
public function getPrice()
{
if(..some condition..){
return ..some value..;
}else{
return parent::getPrice();
}
}
}
Helpers
Helpers can be overridden similarly. In our config.xml file
HTML Code:
<helpers>
<customer>
<rewrite>
<data>Excellence_Test_Helper_Data</data>
</rewrite>
</customer>
</helpers>
PHP Code:
<?php
class Excellence_Test_Helper_Data extends Mage_Customer_Helper_Data
{
}
One important note about overriding is that, we can only override classes which magento creates objects for. For example there are many abstract classes in magento e.g Mage_Catalog_Block_Product_Abstract this class cannot be overwritten. So, if a class is simple extended by magento and its object is not created, overriding won’t work.
Controllers
To override a core controller this is what we do
HTML Code:
<global>
<rewrite>
<test_cart> <!--This can be any unique id -->
<from><![CDATA[#^/checkout/cart/#]]></from> <!-- the URL which u want to override-->
<to>/test/checkout_cart/</to> <!-- destination url -->
</test_cart>
</rewrite>
</global>
Now create a controller at Excellence/Test/controllers/Checkout/CartController.php
PHP Code:
<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class Excellence_Test_Checkout_CartController extends Mage_Checkout_CartController{
}
Few important differences between controller and other rewrites is
- In controller, overriding is done based on URL and not class path.
- You need to require the controller file which you extend
View more threads in the same category:
Bookmarks