Create Global Function In Magento


This code will allow you to add a function that can be called from anywhere within Magento. It extends the helper class

1) Create a file named ‘Mycode.xml’ and copy it to app/etc/modules/ – it should look like this:



    
		
			true
			local
		
    

2) Create the directory
app/code/local/Mycode/Function/etc
and then create a file named ‘config.xml’
In it copy:



    
    
        
            1.0.0
        
    

    
        
        
            
                Mycode_Function_Helper
            
         

    
    

3) Create the directory
app/code/local/Mycode/Function/Helper
and then create a file named ‘Data.php’
In it copy:


You can now call this function like so

		test();
		
		 ?>
,

10 responses to “Create Global Function In Magento”

  1. this is the right way to call the helper

    Mage::helper(‘mycodefunction’)->test();

    to display the result

    echo Mage::helper(‘mycodefunction’)->test();

  2. uhm,.. all i’m seeing are corrections, can you guys post the whole “CORRECT” code for beginners like me? thanks

  3. i have created module same as this solution but it doesn’t work for me. i have the problem with not found in \app\Mage.php on line 547. Anyone else know how to fixed this problem?

  4. To be fair with the author here’s my feedback.

    I don’t see any issue on the code it’s working well, It’s just knowing how to used it.

    If you want to change how you can call the helper just change the to what name you want this helper to be called example you want to call the helper like this echo Mage::helper(‘name’)->test();

    You just need to update the config.xml and change the to
    ———————————————————————–

    Mycode_Function_Helper

    ———————————————————————–
    echo Mage::helper(‘name’)->test();
    ———————————————————————–

    • If you want to change how you can call the helper just change the <function> to what name you want this helper to be called example you want to call the helper like this echo Mage::helper(‘name’)->test();

      You just need to update the config.xml and change the to

      <name>
      <class>Mycode_Function_Helper</class>
      </name>