How To Customize The Magento Core Without Actually Changing It

Magento Core - How to override Magento core

There might be some particular reasons when you have to change the Magento‘s core files then you should be very careful otherwise all your changes made in core files will be lost when the current Magento version is upgraded to latest version. Before changing anything in the core you should device a clear cut policy to ensure that your customized core files are upgradable, non-over writable and easier to accommodate further customizations.

Magento gives the flexibility to customize it’s core but the most important thing is the developers discipline. A slight negligence can make all efforts go in vain especially when the Magento version is upgraded, degraded etc.

Understanding Magento’s Configuration Files

Magento uses merging concept of several files (XML) into one global cached configuration file (XML) just to achieve maximum flexibility of configuration without need for manual editing of configuration files.
Notice how 2 configuration XML files are merged into one global configuration file

Sample 1 (Configuration)

1
2
3
4
5
6
7
<?xml version="1.0"?>
<config>
  <node1>
    <node1_1>Data1</node1_1>
  </node1>
  <node2>Data2</node2>
</config>

Sample 2 (Configuration)

1
2
3
4
5
6
7
8
<?xml version="1.0"?>
<config>
  <node1>
    <node1_2>Data3</node1_2>
  </node1>
  <node2>Data4</node2>
  <node3>Data5</node3>
</config>

Merged file contains both the sample configuration files.

1
2
3
4
5
6
7
8
9
<?xml version="1.0"?>
<config>
  <node1>
    <node1_1>Data1</node1_1>
    <node1_2>Data3</node1_2>
  </node1>
  <node2>Data4</node2>
  <node3>Data5</node3>
</config>

Important Files Used In Magento Configuration

Refer init() method under app/code/core/Mage/Core/Model/Config.php

  1. If usage of config cache is enabled, Magento will try to load the configuration from cache. If loaded successfully, it will skip all the following steps.
  2. Load app/etc/config.xml. This file contains values absolutely necessary for successful load of Magento core components.
  3. Merge all files from app/etc/modules/ (app/etc/modules.xml until 0.6.14100). Files containing modules declarations are dropped here by the core, community and custom packages installed, and are named by package name (Mage_All.xml).
  4. Merge app/etc/local.xml. Contains settings for local database connections, installation date and local encryption key.
  5. If app/etc/local.xml was not found (not installed yet) app/etc/distro.xml is merged. Contains auto-configured values to be used in installation wizard.
  6. Merge config.xml from all the modules that were declared in app/etc/modules/*. These are: app/code/{codePool}/{companyName}/{moduleName}/etc/config.xml

Models

Models are basically set of specific classes to define abstract or resource specific logic. Following code will display how to define a new module functionality:

Lets’ create app/etc/modules/Example_Module.xml:

1
2
3
4
5
6
7
8
9
<?xml version="1.0"?>
<config>
  <modules>
    <Example_Module>
      <codePool>local</codePool>
      <active rel="nofollow" target="_blank">true</active>
    </Example_Module>
  </modules>
</config>

Now lets create app/code/local/Example/Module/etc/config.xml:

1
2
3
4
5
6
7
<?xml version="1.0"?>
<config>
  <global>
    <models>
      <example_mod>
        <class>Example_Module_Model</class>
      </example_mod>

The above code will enable us to use $obj = Mage::getModel('example_mod/test_model') and will return an instance of the class Example_Module_Model_Test_Model, located under app/code/local/Example/Module/Model/Test/Model.php. Notice how example_mod is replaced with Example_Module_Model and together with test_model generate name of required class.

Further Reading: How to Override Magento’s Core Model Classes

Customization

The real beauty lies under customization especially if you are willing to upgrade the code without changing the files manually. Now, if the discipline is followed you can easily override the functionalities without actually changing the core files.

Now, lets take an example where we will try to create a new module In our example we will create a new module called Example_Custom which will further override the Example_Module functionality. Defining this new module is same as app/etc/modules/Example_Module.xml

app/code/local/Example/Custom/etc/config.xml:

To ensure that the application won’t break we will provide all classes in our custom module that exists in our original module.

1
2
3
4
5
6
7
<?xml version="1.0"?>
<config>
  <global>
    <models>
      <example_mod>
        <class>Example_Custom_Model</class>
      </example_mod>

Now lets override only one class to see how it works

1
2
3
4
5
6
7
8
9
<?xml version="1.0"?>
<config>
  <global>
    <models>
      <example_mod>
        <rewrite>
          <test_model>Example_Custom_Model_Test_Model</test_model>
        </rewrite>
      </example_mod>

now our app/code/local/Example/Custom/Model/Test/Model.php will look something like this:

1
2
3
4
5
6
7
8
9
<?php
 
class Example_Custom_Model_Test_Model extends Example_Module_Model_Test_Model
{
  public function exampleMethod()
  {
    return 'custom result';
  }
}

Now, everywhere in the code $obj = Mage::getModel('example_mod/test_model') will return an instance of Example_Custom_Model_Test_Model with all original functionality of Example_Module_Model_Test_Model and custom exampleMethod.

Helpers

Helper are the support classes for performing some specific manipulations with data in *.phtml templates or in *.xml layouts i.e.

1
2
3
$this->helper('wishlist'); // wishlist module; data.php helper
$this->helper('catalog/image'); //catalog module; image.php helper
$this->helper(); //current module; data.php helper

Helpers are also useful for caching purposes (refer to core/Mage/Core/Helper/Abstract.php)

Resources

Resources are overloaded the same way as we overload models. These are the data persistence resources used by the system’s data mapper. Also business logic for model collections are encapsulated in collection resources. All data access funnels down thru the resources from the models (or controllers, if business logic is being defined in a page controller )

Admin Menu and ACL

Admin menu is setup in app/code/core/Mage/Adminhtml/etc/config.xml Copy & paste an existing XML chunk and customize as per your requirements. You can supposedly put this in any other configuration file ( think a user created module ) & have the XML merged in.

Magento Customization How To’s

I am still drafting my howto’s for customizing Magento. Just, stay tuned for my next post where i will focus on how to customize Blocks, Controllers and Themes.

Please leave me a comment and let me know your own experiences of Overriding the Magento core. Subscribe our RSS to receive latest Magento updates.

  • Anonymous

    customizing magneto seems quite complex.

  • http://www.websoftwareoutsourcing.com/ iphone apps developer india

    I am really impressed by this excellent stuff. I always prefer to read the quality content and this thing I found in you post. Thanks for sharing.

  • http://friv-10.y8u.org/ Friv 10

    Your
    content has given me reason to think and this is my favorite kind of
    material.