Overriding Helpers in Magento
How to override helpers in Magento?
To override any helper you need to make changes in two files.
- module/etc/config.xml
- module/helper/Data.php
Assume, Package is the name of your custom package,
Module is the name of your custom module,
CorePackage is the Package name of core Module that you want to override,
CoreModule is the core Module that you want to override
Changes in config.xml file of your module
Write this to config file of your module.
module/etc/config.xml
<?xml version="1.0"?> <config> <modules> <Package_Module> <version>0.1.0</version> </Package_Module> </modules> <!--overriding helper--> <global> <helpers> <coreModule> <rewrite> <data>Package_Module_Helper_Data</data> </rewrite> </coreModule> </helpers> </global> </config>
Changes in Data.php file of your module/helper
Write this to Data.php file of your module.
module/helper/Data.php
<?php class Package_Module_Helper_Data extends CorePackage_CoreModule__Helper_Data { //your code } ?>
Example to demostrate how to override helper/Data.php file of Customer Module present in core Mage Package
Suppose you want to override helper/Data.php of Mage package which is a part of core file in Magento. Of course you can override any helper class in core file using this method.
Go to etc/config.xml file of your module and add the following within <global> tag.
<?xml version="1.0"?> <config> <modules> <syntaxpage_customer> <version>0.1.0</version> </syntaxpage_customer> </modules> <!--overriding helper--> <global> <helpers> <customer> <rewrite> <data>Syntaxpage_Customer_Helper_Data</data> </rewrite> </customer> </helpers> </global> </config>
Add the helper that you want to override.
syntaxpage/customer/helper/Data.php
<?php class Syntaxpage_Customer_Helper_Data extends Mage_Customer_Helper_Data { { //your code } ?>