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:

<?xml version="1.0"?>
<config>
    <modules>
		<Mycode_Function>
			<active>true</active>
			<codePool>local</codePool>
		</Mycode_Function>
    </modules>
</config>

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

<?xml version="1.0"?>
<config>
 
    <modules>
        <Mycode_Function>
            <version>1.0.0</version>
        </Mycode_Function>
    </modules>
 
    <global>
 
        <helpers>
            <function>
                <class>Mycode_Function_Helper</class>
            </function>
        </helpers> 
 
    </global>
 
</config>

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

<?php
 
class Mycode_Function_Helper_Data extends Mage_Core_Helper_Abstract
{
 
	public function test(){
 
	return 'works';
 
	}
 
}

You can now call this function like so

		<?php 
 
		 echo Mage::helper('function')->test();
 
		 ?>

10 thoughts on “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. Hi Bones, this is the exact code I used on a project and it worked….I never bothered to test the ‘corrections’ others have posted, but I left them in case it would help someone else…If you have any doubts, check here: http://www.e-commercewebdesign.co.uk/blog/magento-tips/custom-global-functions-in-magento.php

    I would think this site has much more traffic than my own, and if there were faults in the code it would have been pointed out by now. Good Luck!

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

  5. 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();
    ———————————————————————–

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

Leave a Reply

Your email address will not be published.