In this post it will be presented how you should overwrite core Models, Blocks, Helpers and Controllers in Magento. There are several ways how this functionality can be accomplished but this is the recommended way that should be followed.

The easiest and fastest solution to do this is to copy the core class inside your local pool, following the original file hierarchy. This is possible because Magento's local pool is loaded before the core pool. ( The directory hierarchy for class loading is as follows: local, community, core and lib). As it was mentioned earlier this is not an elegant solution, it has a lot of drawbacks, the whole content of file must be copied and at upgrades a lot of refactoring is required.

The elegant way to do rewrites it is enumerated below:

1. Models Rewrite

In order to rewrite a model in Magento all you have to do is to specify in your modules configuration file between the <global><models> tag the name of the module you want to rewrite, and wrap it around with the rewrite tag and specify the new class that will replace the old one. Let's take a look at one specific exampe to understand it much easier. In order to rewrite the catalog/product model (Mage::getModel('catalog/product')) we have to use the following xml snippet in our configuration.

...
<models>
    <catalog>
         <rewrite>
             <product>Namespace_Module_Model_Product</product>
          </rewrite>
     </catalog>
</models>
...

In the prevoius example instead of Namespace_Module_Model_Product you will have to use your models namespace. Now when you will use Mage::getModel('catalog/product') it will return Namespace_Module_Model_Product class. Make sure that inside of the new class you will extend the original model.

<php
class Namespace_Module_Model_Product extends Mage_Catalog_Model_Product {
....
}
?>

 

2. Blocks Rewrite

In order to rewrite a block in Magento all you have to do is to specify in your modules configuration file between the<global><blocks> tag the name of the block you want to rewrite, and wrap it around with the rewrite tag and specify the new class that will replace the old one. Let's take a look at one specific exampe to understand it much easier. In order to rewrite the catalog/product_view block we have to use the following xml snippet in our configuration.

...
<blocks>
    <catalog>
         <rewrite>
             <product_view>Namespace_Module_Block_Product_View</product_view>
          </rewrite>
     </catalog>
</blocks>
...

In the prevoius example instead of Namespace_Module_Block_Product_View you will have to use your models namespace. Make sure that inside of the new class you will extend the original model.

<php
class Namespace_Module_Block_Product_View extends Mage_Catalog_Block_Product_View {
....
}
?>


You can test it the rewrite works:

$object = Mage::app()
       ->getLayout()
       ->createBlock('catalog/product_view');

echo get_class($object); // it should return Namespace_Module_Block_Product_View

3. Helpers Rewrite

In order to rewrite a helper in Magento all you have to do is to specify in your modules configuration file between the<global><helpers> tag the name of the helper you want to rewrite, and wrap it around with the rewrite tag and specify the new class that will replace the old one. Let's take a look at one specific exampe to understand it much easier. In order to rewrite the customer/address helper we have to use the following xml snippet in our configuration.

...
<helpers>
    <customer>
         <rewrite>
             <address>Namespace_Module_Helper_Address</address>
          </rewrite>
     </customer>
</helpers>
...

In the prevoius example instead of Namespace_Module_Helper_Address you will have to use your models namespace. Make sure that inside of the new class you will extend the original model.

<php
class Namespace_Module_Helper_Customer_Address extends Mage_Customer_Helper_Address {
....
}
?>


You can test it the rewrite works:

$object = Mage::helper('customer/address');

echo get_class($object); // it should return Namespace_Module_Helper_Customer_Address

 

4. Controllers Rewrite

When we are talking about controller overrides in Magento we can separate the admin controllers and the frontend controllers.

In order to rewrite an admin controller in Magento all you have to do is to specify in your modules configuration file the following:

...
<admin>
    <routers>
         <adminhtml>
             <args>
                 <modules>
                     <Namespace_Module before="Mage_Adminhtml">Namespace_Module</Namespace_Module>
                 </modules>
             </args>
          </adminhtml>
     </routers>
</admin>
...

 

All you have to do next is to create the new controller in your module and extend the controller you want to override. For example if we want to change some behaviour in Mage/Adminhtml/controllers/Catalog/ProductController.php we can do the following:

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Adminhtml') . DS . 'Catalog' . DS . 'ProductController.php';
//require_once 'Mage/Adminhtml/controllers/Catalog/ProductController.php';
class Namespace_Module_Catalog_ProductController extends Mage_Adminhtml_Catalog_ProductController {

    public function newAction() {
         echo 'We have rewritten core newAction';
         die;
    }

}


In order to rewrite a frontend controller in Magento, let's say the Mage_Catalog_CategoryController all you have to do is to specify in your modules configuration file the following:

...
<frontend>
    <routers>
         <catalog>
             <args>
                 <modules>
                     <Namespace_Module before="Mage_Catalog">Namespace_Module_Catalog</Namespace_Module>
                 </modules>
             </args>
          </catalog>
     </routers>
</frontend>
...

 

Now all you have to do is to create inside your modules controller directory the Catalog directory and add the CategoryController.php file with the following sample content.

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Catalog') . DS . 'CategoryController.php';
//require_once  'Mage' . DS . 'Catalog' .DS . 'controllers' . DS . 'CategoryController.php';
class Namespace_Module_Catalog_CategoryController extends Mage_Catalog_CategoryController{

    public function viewAction() {
         echo 'We have rewritten  the view Action';
         die;
    }

}

 

I hope the post is useful, happy coding ;)