Magento Module Development Series – Part10 – Overriding

Overriding
In this blog we are going to lean about overriding of magento core classes. What this means, is that we tell magento to use our module class’s instead of a magento core classes.

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

<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
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
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

<models>
        	<catalog>
        		<rewrite>
        			<product>Excellence_Test_Model_Product</product>
        		</rewrite>
        	</catalog>
</models>

and our class would be

<?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

<helpers>
            <customer>
            	<rewrite>
                	<data>Excellence_Test_Helper_Data</data>
                </rewrite>
            </customer>
</helpers> 
<?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

    <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
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
  • Anonymous

    hello,
    please see the above block section it contains spelling mistake in rewrite tag…which makes beginner in trouble while implementing..

    • Manish Prakash

      can you please point out which spelling mistake?

  • Anonymous

    in above Blocks and Models section  instead of it has written in xml code

  • http://www.facebook.com/profile.php?id=1481783454 Kuih Abuk Abuk

    I feel this implemention from fontis is easier

    for review you just take the review_entity_summary field 

    http://www.fontis.com.au/blog/magento/sort-products-rating

    Hisham
    http://Seamug.com

  • Saurabh Sutariya

    Hello,

    To Override Core Controller,,

    Companyname_Modulename

    It’s really easy and working fine.

  • Amit Maurya

    Please put an example of override orderController.php of backend …. i did block are working but orderController is not working…

  • Amit Maurya

    here is my module config file…

    0.1.0

    Ambr_Adminhtml_Block_Sales_Order_Grid
    Ambr_Adminhtml_Block_Sales_Order_View

    /adminhtml/sales_order/

    Ambr_Adminhtml

  • Amit Maurya

    controller not loaded

  • Akram SALHA

    Hello,

    When you say config.xml, there are tens of config.xml, which one you mean? Can you please be more specific?

    • Mohammad Faisal

      as it is part of series of blogs, so need to pay attention to the previous blogs.
      here config.xml is located at “app/code/local/Excellence/Test/etc/config.xml”