0% found this document useful (0 votes)
74 views3 pages

Override Magento Core Files

This document discusses two methods for overriding Magento core files: 1. Copying the core file to the local code pool folder while maintaining the file path. Magento will then load the local file instead of the core file. This is a simple way to override files while testing or developing a module. 2. Creating a module that rewrites the core class using XML. The class would extend the core class and override specific methods. This approach organizes the custom code into a module and makes it upgrade friendly. It also allows overriding models, blocks, helpers and controllers in Magento.

Uploaded by

Santosh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views3 pages

Override Magento Core Files

This document discusses two methods for overriding Magento core files: 1. Copying the core file to the local code pool folder while maintaining the file path. Magento will then load the local file instead of the core file. This is a simple way to override files while testing or developing a module. 2. Creating a module that rewrites the core class using XML. The class would extend the core class and override specific methods. This approach organizes the custom code into a module and makes it upgrade friendly. It also allows overriding models, blocks, helpers and controllers in Magento.

Uploaded by

Santosh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Override Magento Core Files – Simplest Way

Magento, Tips and Snippets Mar 3 rd, 2014 2 Comments

There are several methods used for overriding magento core files, which i have explained earlier. Here I’m
going to explain one of simplest method to override core files in magento.

In this method to override magento core files, you don’t need to create your own module and write some xml in
config.xml. Instead you can simply copy core file (which you want to override) and paste it in
‘app/code/local/Mage’ maintaining the same path of that php file. Then you can start editing this file, magento
will read your file instead of the core file.
Below is an example :

=> If you would like to override ‘app/code/core/Mage/Catalog/Block/Product.php’ you need simply need it to
put Product.php in ‘app/code/local/Mage/Catalog/Block/Product.php’. Doing only this will let magento read
this file from local folder instead of core folder.

Reason for such behavior of reading from local folder to core folder is :
This process is called overriding Magento core functionality and is based on the fact that Magento sets its PHP
include paths to first look in app/code/local/ then app/code/community/ and finally in app/code/core/. This has
the effect that any files of the same name placed under the local or community name space will take precedence
in loading, hence, we can override almost any core file in this way.

Demerits of Using Above Approach :


1. For one thing, we must override the complete core file and copy all the class functions. Once the overridden
file is in place, this will be the file will be used instead of magento core file always. Given that most core
classes contain several and many times a large number of methods it means that we are effectively overriding
all those methods in our file.
2. This approach is not magento upgrade friendly, because of the above reasons.
3. This approach doesn’t work for controllers.

This approach is only useful while your testing/developing your module. Instead of writing a whole
module, you can quickly override the core class see if things work well.

Magento Module Development Series – Part 10 – Overriding


Magento, Module Development Series Sep 22 nd, 2011 11 Comments
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 whenever 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

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

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

You might also like