Skip to content
NewRedesigned blogFeedback
Jigar Karangiya

Magento 2: How to use helper in PHTML?

Magento 2, an open-source e-commerce platform, offers a variety of tools for developers to create robust online stores. One such tool is the Helper class, a collection of methods that can be used globally.

Magento 2: How to use helper in PHTML?
Written by
JKJigar Karangiya
Published
Reading time
2 min read

Jigar Karangiya · · 2 min read

Magento 2, an open-source e-commerce platform, offers a variety of tools for developers to create robust online stores. One such tool is the Helper class (opens in new tab), a collection of methods that can be used globally.

PHTML is a file extension for PHP files in Magento 2, and it’s where the HTML and PHP logic for a view is stored.

Creating a Helper in Magento 2

Helpers in Magento 2 are classes that contain methods which can be accessed from any location in your code. They are typically used for functions that are widely used throughout your codebase.

Here’s an example of how to create a helper:

php
<?php
 
namespace Vendor\Module\Helper;
 
use Magento\Framework\App\Helper\AbstractHelper;
 
class Data extends AbstractHelper
{
    public function customHelperFunction($arg1, $arg2)
    {
        return "Hello World";
    }
}

In this code:

  • We define a namespace (**Vendor\Module\Helper**) to avoid naming conflicts.
  • We extend the **AbstractHelper** class provided by Magento 2.
  • We define a method **customHelperFunction** that takes two arguments ($arg1, $arg2). This is where you’ll put the logic for your helper function.

Remember to replace Vendor\Module it with your actual vendor and module name.

Using the Helper in a PHTML File

Once you’ve created your helper, you can use it in a PHTML file like this:

php
$helper = $this->helper('Vendor\Module\Helper\Data');
echo $helper->customHelperFunction('arg1', 'arg2');

In this code:

  • We use the $this- >helper() method to get an instance of our helper class. The argument for this method is the fully qualified name of our helper class.
  • We then call the **customHelperFunction** method on our helper instance, passing in the arguments '**arg1** ' and '**arg2** '.

Conclusion

Helpers in Magento 2 are a powerful tool for developers, allowing you to write reusable code that can be accessed from anywhere in your application. By creating a helper and using it in a PHTML file, you can make your Magento 2 development more efficient and maintainable.

I hope this tutorial finds you helpful, See you in the next blog.

You may also like :

How to Remove Sample Data from Magento 2

Create CMS Block Programmatically Magento 2

Patch to Debug Blocks and Containers in Magento 2

Keep reading
All posts

01 · 2 min · Shares Adobe Commerce Core, Quick Notes

Magento 2 UI Components and Visibility Conditions

Have you ever wanted to show or hide certain parts of your Magento store based on specific conditions? For example, showing special offers only to VIP customers or displaying different content in different store views?

2 min

02 · 3 min · Shares Adobe Commerce Core, Quick Notes

Deep Dive into Magento 2 Events and Observers

In this tutorial, I will guide you through the popular events and observers which works based on event driven architecture which is also referred as publish-subscribe pattern.

3 min

03 · 2 min · Shares Adobe Commerce Core, Quick Notes

How to Remove Sample Data from Magento 2

Discover the step-by-step process of removing sample data from your Magento 2 installation in my latest blog post. I’ll guide you through accessing your server, navigating to the Magento 2 directory, and using Magento’s command-line tool to remove sample data.

2 min

Newsletter

New Magento tutorials, straight to your inbox

Code examples, performance tips, and fixes for common Magento issues. No spam.

Subscribe