Categories: Magento Tutorials

How To Create A Magento Helper

Disclaimer: Magenticians does not necessarily agree with the views expressed in this guest post. They are presented to bring to light all diverse views in the Magento and general ecommerce community.

Due to some good reasons, it’s a bad programming habit, and it’s not recommended at all if you modify the core files of Magento. But sometimes, you may want to add new classes or override different functions in your Magento module. Therefore, Magento came up with the Helpers that are the right entity to fulfill your needs. A Helper in Magento is an object that contains practical methods. You can call it in template files, controllers, models or anywhere in Magento. All you need is to load your helper like this: Mage::helper(‘MODULE_NAME/HELPER_CLASS’)->HELPER_FUNCTION(); Creating a Magento Helper is quite easy. In this tutorial, you will learn how to create a helper in Magento. You will also find out how to use a helper! Assume that you have a simple Testmodule under MAGENTO_ROOT/app/code/local/Testextension/Testmodule. If you create a new module, do not forget to activate it by defining it in app/etc/modules directory. In your module’s ../etc/config.xml file, declare the new helper class under <global> as follows:
<config>
 ...
 <global>
  ...
  <helpers>
   <testhelper> <!-- helper name -->
    <class>Testmodule_Testhelper_Helper</class> <!-- declaration of helper class -->
   </testhelper>
  </helpers>
 </global>
</config>
Then create a new folder Helper in MAGENTO_ROOT/app/code/local/Testextension/Testmodule/ and a new helper class file Testhelper.php in newly created Helper folder. The class structure will be:
<?php
 class Testextension_Testmodule_Helper_Testhelper extends Mage_Core_Helper_Abstract
 {
  public function add($num1, $num2)
  {
   return $num1 + $num2;
  }
  public function subtract($num1, $num2)
  {
   return $num1 - $num2;
  }
  public function multiply($num1, $num2)
  {
   return $num1 * $num2;
  }
  public function divide($num1, $num2)
  {
   return $num1 / $num2;
  }
 }
Now we can use this helper function anywhere in Magento like this:
Mage::helper('testmodule/testhelper')->add(1,2);
Mage::helper('testmodule/testhelper')->subtract(5,3);
Mage::helper('testmodule/testhelper')->multiply(10,5);
Mage::helper('testmodule/testhelper')->divide(30,6);
We’re done! I hope you properly understood how to create a Magento Helper. I would suggest you to keep practicing; this is the only way to improve yourself. :-) Want to ask any questions? Feel free to leave a comment.
Fayyaz Khattak

Fayyaz is a Magento Community Manager at Cloudways - A Managed Magento Hosting Platform. He contributes as a Guest Author at Magenticians to share his knowledge with the Magento Community. Fayyaz is a food lover and enjoys driving.

View Comments

Share
Published by
Fayyaz Khattak

Recent Posts

Building Employee Trust and Dedication – A Complete Guide

In the highly competitive modern workplace, trust, and employee loyalty are crucial factors for long-term… Read More

3 months ago

12 Winning Strategies for Small Businesses Marketing

In the ever-evolving world of small business developing and implementing effective marketing strategies is critical to… Read More

3 months ago

Top 10 App Development Companies in Dubai 2024

With one of the highest internet penetration rates, the UAE has set out to revolutionize… Read More

3 months ago

Transforming Industries: How Amazons GPT44X is Revolutionizing AI Technology

Artificial Intelligence (AI) has been continually evolving, leading to remarkable advancements in various industries. Among… Read More

8 months ago

Top Magento 2 Extensions for 2023

Extensions, extensions and lots of extensions. We all love extensions, don’t we? After all, extensions… Read More

11 months ago

Unleashing the Power of Software Testing: Cooperating with a Testing Firm

Software quality is crucial to a firm's success across industries in the quickly changing digital… Read More

11 months ago