Magento - create a global function

I want to create a function that can be accessed by all * .phtml files. Where should I put this function in a magento framework?

+4
source share
5 answers

You must create a module and helper class in this module (usually MyCompany_Mymodule_Helper_Data by default). Then add your function to this helper class. You can go to this function in PHTML as follows:

 Mage::helper("mymodule")->someFunction(); 

Hope this helps!

Thanks Joe

+5
source

For empty and dirty things, you can always define it in index.php. For example, I always set this function:

 function dumpit($obj) { print '<pre>'; print_r($obj); print '</pre>'; } 

Then you can quickly call this subroutine from anywhere in the world without thinking about all the other utility function names falling into the helper.

+6
source

C:\wamp\www\mydirectory\app\code\core\Mage\Page\Helper\Data.php is my way. I used the print_r function as the pr() function.

Put it in Data.php as shown below.

 class Mage_Page_Helper_Data extends Mage_Core_Helper_Abstract { function pr($data) { echo "<pre>"; print_r($data); echo "</pre>"; } } 

where the page is mymodule.

Call it from any .phtml file using

 Mage::helper("page")->pr($abcd); 

Hope this helps.

+1
source

For anyone interested, I put together a short tutorial on how to create a global function in Magento: http://joe-riggs.com/blog/2011/06/create-global-function-in-magento/

+1
source

I just CANNOT SAVE with overly bloated nightmares that are Magento modules and blocks, and so I used @Billy's answer earlier , as it is just with a little bit of involvement.

Unfortunately, this solution does not seem to work in the local server environment. After a bit more research, I found that I can post my global functions here:

.../app/code/local/Mage/Core/functions.php

Finally, I read that this is not a completely updated option , but it works for me.

0
source

Source: https://habr.com/ru/post/1337059/


All Articles