Wednesday , December 19 2018
Home >> All tutorial >> Magento Developers’ Guide – Part 12: The Helpers

Magento Developers’ Guide – Part 12: The Helpers

Now, Magento Tutorial will show you the way to create your own helper in Magento.

1. What’s a helper?

“Helper” is an object that will contain practival functions for you and you can call it from anywhere, you just load your helper to use it. For example:

  1. $helper = Mage::helper(‘monhelper’);
Note that it’s equivalent to :
  1. $helper = Mage::helper(‘monhelper/data’);
Indeed, it is the default helper « data » wich is called.
2. Create your own helper
As you are now used to, it starts by declaring it in the config.xml of your plugin, do that in <global> and after </blocks>
  1. <helpers>
  2.    <test>
  3.           <class>Pfay_Test_Helper</class>
  4.     </test>
  5. </helpers>
Then creates the folder and the file app/code/local/Pfay/Test/Helper/data.php function wich will contains the function bytwo($ nbr) with a number as an argument and returns that number multiplied by two. A helper is an object that extends the core class Mage_Core_Helper_Abstract.
  1. <?php
  2. class Pfay_Test_Helper_Data extends Mage_Core_Helper_Abstract{
  3.    public function bytwo($nbr){
  4.         return $nbr*2;
  5.    }
  6. }
Now in/app/code/local/Pfay/Test/Block/monblock.php we change the function by:
  1. <?php
  2. class Pfay_Test_Block_Monblock extends Mage_Core_Block_Template
  3. {
  4.       public function methodblock()
  5.       {
  6.             $helper = Mage::helper(‘test’);
  7.             return ‘2*2 = ‘.$helper->bytwo(2);
  8.       }
  9. }
Now when we go on, we see that it works!
Tutorial by:


Here is the library of Magento Download for FREE (all versions)

Your Magento website needs a powerful Magento ERP for stock control.


magento erp

About Champ

Leave a Reply

Your email address will not be published. Required fields are marked *

Heap | Mobile and Web Analytics