Hello Magento Friends,

Hope all are doing well. Today I am going to explain Magento 2: How to Call phtml File in Helper. In case you missed out our previous blog, check it out here. Magento 2: How to Create/Add Custom Cache Type in Cache List.

Magento 2 E-commerce platform allows adding functionalities to your website. But changing the core files of Magento 2 is not a good practice. For this, the Magento helper class is used to override the core files. To add a phtml file from helper then the below code is useful. Let’s look at the steps to Call the phtml File in Helper in Magento 2.

Steps to Call phtml File in Helper in Magento 2:

Step 1: First you need to add Data.php in the following path:

app\code\Vendor\Extension\Helper

Then, add the below code

Step 2: Now you need to add the Extension.php file in the following path:

app\code\Vendor\Extension\Block

And add the below code

Step 3: After this now you need to add the extension.phtml in the following path:

app\code\Vendor\Extension\view\frontend\templates

And finally, add the below code:

That’s it!

Conclusion:

Hence, this way you can call phtml file in helper in Magento 2. While implementing the above code, if you come across any hardship, let me know in the comment section. Do share the article further and stay connected for more updates.

Happy Coding!

[Total: 8   Average: 5/5]