-
Notifications
You must be signed in to change notification settings - Fork 3
Extending Magento Core Functionality
There are times when you'll discover a function in the Magento core code pool (in app/code/core) that you'll need to modify. Since these files are not tracked in Git, you'll need to provide this functionality in the local code pool (in app/code/local) instead.
This is accomplished by creating a new module of the same name, but in a different namespace. For example, if you're adding functionality to a class in the Mage_Checkout module, you would create a new module called Totsy_Checkout.
-
Create a module directory in
app/code/local/Totsy/<modulename>. -
Add the
0.1.0etc/config.xmlfile in that module: -
Update the
app/etc/modules/Totsy_All.xmlfile to include your new module:<Totsy_[modulename]> true local </Totsy_[modulename]>
Now that you've created a module for your new functionality, create a new class in your module in the same directory/path as the class in the core code pool.
For example, an update to class Mage_Checkout_Block_Cart in app/code/core/Mage/Checkout/Block/Cart.php would be in a new class called Totsy_Checkout_Block_Cart in app/code/core/Totsy/Checkout/Block/Cart.php. This new class should extend the original class, and override only the functions that need to be updated.
The class method implementation can be copied from the parent class, if it needs to be modified directly. Otherwise, the parent class' implementation should be called before or after performing additional work in the class function, using parent::<methodName>().
Once you've created a new child class that extends from a core class, you need to indicate to Magento's factory mechanism that it should instantiate your class instead of the core class when needed. This is accomplished by adding a rewrite in your module's etc/config.xml for each type of class (block, model, helper).
Inside the config/global element:
<blocks>
<checkout>
<rewrite>
<cart>Totsy_Checkout_Block_Cart</cart>
</rewrite>
</checkout>
</blocks>
This will force Magento to use your class whenever a block with group name 'checkout/cart' is requested (such as Mage::getBlock('checkout/cart').
You've effectively replaced a core class system-wide with your own, but the original functionality is still provided by the parent (original) class.
There is a highly experimental module configuration tool also available, that will simply execute each of the steps outlined above. Written by genius software developer Tharsan Bhuvanendran, the command line tool is a PHP script located at dev/moduleconf.php. For more information, just invoke the tool at a command prompt:
$ php dev/moduleconf.php
Usage:
moduleconf <modulename> <classtype> <classname>
<modulename> is the name of the new module. Supplying this argument alone will simply create an empty module, and register it in the module directory (in `app/etc/modules/Totsy_All.xml`).
<classtype> can be either `controller`, `model`, or `helper` (case insensitive).
<classname> is the name of the new class.
This tool will initialize modules (creating the module directory, and registering it in app/etc/modules/Totsy_All.xml), create bare classes with the appropriate full names, and include a PHP docblock in the header.
Additionally, when creating a new class that overrides one that exists in another module in the core code pool, it will also generate the necessary <rewrite> tags in the module configuration file, and the corresponding 'extends' clause to the class definitions.