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