Tools and Resources for all Your Ecommerce Pain Points
Explore Now

Learn How to Use Magento 2 Helper

To add more functionality for your Magento 2 powered store, modifying the core files of the platform is a bad approach. So, in order to extend the current or add new functionalities in Magento 2, the best approach is to create a custom module and use Magento 2 Helper which gives you one of the best opportunities to override core files and pre-defined functions of the Magento 2 platform.

What is Magento 2 Helper?

The Magento 2 helper is the class which is used for adding functionalities to several features and can be used anywhere in the entire website. They are created as Singleton (single instances of objects) which can be called in controllers, views, models, templates etc.

So today in this guide, I am going to teach you how to use Magento 2 helper.

Let’s assume that you have to show account login and register links along with the Add to Cart button to your guest users.

Let’s start by creating a custom Magento 2 module!

Create Magento 2 Custom Module

Add module.xml file in app/code/Magenticians/Mymodule/etc and copy the following code in it:

Add registration.php in app/code/Magenticians/Mymodule and copy the following code in it:

Create Magento 2 Helper

Now add Data.php file in app/code/Magenticians/Mymodule/Helper and copy the following code in it:

In the code (helper) above, I have created a function isLoggedIn()  to get the status of logged in users.

Override addtocart.phtml

Now go to vendor/magento/module-catalog/view/frontend/templates/product/view from the root directory of your store and you will see the addtocart.phtml file. Copy the file and paste it into app/code/Magenticians/Mymodule/view/frontend/templates/catalog/product/view.

Add catalog-product.view.xml file in app/code/Magenticians/Mymodule/view/frontend/layout and copy the following code in it:

I have created the above file to override the core addtocart file with our custom addtocart file.

To show the login and register option to the guest users, go to addtocart.phtml from app/code/Magenticians/Mymodule/view/frontend/templates/catalog/product/view and and copy the following code in it:

In the above code, I have set the condition that if the user is not logged in, show the register and login links.

The final addtocart.phtml will be:

Run CLI Commands

Launch SSH terminal and go to the root directory of your Magento 2 store.

Then run the following commands:

Now go to the product page of your store and logged in as a customer, you will see the desired result:

Magento 2 helper result

It means that Magento 2 helper has been successfully implemented.

Wrapping Up

I have shown you an example on how to use Magento 2 helper, hence you can extend it according to your needs and requirements. However, if you still face issues in its implementation, then just drop your comment below and I’ll try to respond asap!

If you are having trouble with Helper in Magento 1.x, do see:  How To Create A Magento Helper.

 

About Author

Syed Muneeb Ul Hasan is an expert in PHP and Magento, he prefers to educate users in implementing and learning Magento. When not working, he loves to watch cricket.