
MAGENTO 1.X – CREATE A CUSTOM HELPER
We are going to assume that you have a local extension working under app/code/local/Offset101/Helpertest, so, we will proceed adding our helper, as follows:
- Add the below in within the global tag in app/code/local/Offset101/Helpertest/etc/config.xml:
12345<helpers><offset101_helpertest><class>Offset101_Helpertest_Helper</class></offset101_helpertest></helpers> - Create a file under app/code/local/Offset101/Helpertest/Helper/Data.php that will be as a generic helper method:
12345678<?phpclass Offset101_Helpertest_Helper_Data extends Mage_Core_Helper_Abstract{public function helloHelper(){echo "Hello World!!!";}} - Create a file app/code/local/Offset101/Helpertest/Helper/Calculator.php made for particular methods helper:
12345678910111213141516171819<?phpclass Offset101_Helpertest_Helper_Calculator extends Mage_Core_Helper_Abstract{public function sum($a, $b){return $a + $b;}public function substraction($a, $b){return $a - $b;}public function multiplication($a, $b){return $a * $b;}public function division($a, $b){return $a / $b;}} - Right now your helper is done, you can call it from wherever you want on Magento such as Controllers, Models, Template files, etc. You can do so, like the below example:
12345678# Call methods on the Data.php generic helper fileMage::helper('offset101_helpertest')->helloHelper();# Call methods on the Calculator.php helper fileMage::helper('offset101_helpertest/Calculator')->sum(2,3);Mage::helper('offset101_helpertest/Calculator')->substraction(10,3);Mage::helper('offset101_helpertest/Calculator')->multiplication(5,3);Mage::helper('offset101_helpertest/Calculator')->division(9,3);