Skip to content
NewRedesigned blogFeedback
Jigar Karangiya

How to Create a Helper Class in Magento 2?

If Magento 2 was a city, helper classes would be the local guides. They don’t always make the news, but when you’re lost, they’re the first ones you call.

How to Create a Helper Class in Magento 2?
Written by
JKJigar Karangiya
Published
Reading time
2 min read

Jigar Karangiya · · 2 min read

If Magento 2 was a city, helper classes would be the local guides.
They don’t always make the news, but when you’re lost, they’re the first ones you call.

A helper class in Magento 2 is basically your reusable code library — a safe little folder of logic that you can pull out anytime without rewriting the same function five times.
It’s like having that one colleague who always remembers the Wi-Fi password.


What is a Helper Class in Magento 2?

A helper class is just a PHP class that usually extends Magento’s [AbstractHelper](https://github.com/magento/magento2/blob/2.4-develop/lib/internal/Magento/Framework/App/Helper/AbstractHelper.php) and stores functions you’ll use in multiple places.

Common uses include:

  • Fetching config values (instead of scrolling through XML files at midnight)
  • Formatting prices, dates, or other data
  • Storing logic you really don’t want to rewrite later

When Should You Use a Helper Class?

Think of this rule:

If you’ve copied and pasted the same code twice , it’s time to move it into a helper.


Creating a Helper Class in Magento 2

Let’s build a helper from scratch.


Step 1: Create the File

Inside your custom module:

php
app/code/Vendor/Module/Helper/Data.php
 
 
<?php
namespace Vendor\Module\Helper;
 
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\ScopeInterface;
 
class Data extends AbstractHelper
{
    public const XML_PATH_CUSTOM = 'custom_section/general/enable';
 
    /**
     * Get any config value
     */
    public function getConfigValue($field, $storeId = null)
    {
        return $this->scopeConfig->getValue(
            $field,
            ScopeInterface::SCOPE_STORE,
            $storeId
        );
    }
 
    /**
     * Check if our feature is enabled
     */
    public function isCustomEnabled($storeId = null)
    {
        return $this->getConfigValue(self::XML_PATH_CUSTOM, $storeId);
    }
}

Step 2: Use Your Helper in Another Class

php
protected $helperData;
 
public function __construct(
    \Vendor\Module\Helper\Data $helperData
) {
    $this->helperData = $helperData;
}
 
// Example usage:
if ($this->helperData->isCustomEnabled()) {
    // Do something..
}

Why Helpers Are Awesome

  • They make your code clean
  • They make your code reusable
  • They make your code easier to debug

Magento 2 helper classes are a fundamental tool for organizing reusable code.
They allow you to keep logic centralized, improve code maintainability, and reduce duplication.
By following best practice such as keeping helpers concise, avoiding hardcoded values, and using dependency injection—you can ensure your Magento 2 projects remain scalable and easy to manage.

That’s it, give me comments full of helper usecases.

You may also like,

How to Get Store Configuration value in Magento 2?

Magento 2 – How to get current Store ID Programmatically?

How to Create a Custom Cron Group in Magento 2?

Keep reading
All posts

01 · 10 min · Shares Adobe Commerce Core

Adobe Commerce Agentic Capabilities, Q4 2026

Five agentic releases for Adobe Commerce: LLM product discovery, Brand Visibility, the Shopper MCP server, Brand Concierge, and Experience Workspace. What each one does, which editions get it, and when.

10 min

03 · 3 min · Shares Adobe Commerce Core

Conditions for Collections in Magento 2

Collections in Magento 2 (Now Adobe Commerce), are among the most powerful tools for retrieving and manipulating data directly from the database.

3 min

Newsletter

New Magento tutorials, straight to your inbox

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

Subscribe