{"id":599,"date":"2019-09-20T06:45:30","date_gmt":"2019-09-20T06:45:30","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/09\/20\/call-helper-function-in-phtml-in-magento-2\/"},"modified":"2025-05-22T15:57:12","modified_gmt":"2025-05-22T10:27:12","slug":"call-helper-function-in-phtml-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/call-helper-function-in-phtml-in-magento-2\/","title":{"rendered":"How to Call Helper Function in phtml in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Helper classes offer functionalities for many features in Magento 2 stores. With Magento 2, the Helper can be called in controllers, models, views and other helpers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The below code can be used when you want to have helper\u2019s data in .phtml file in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Making changes in the default Magento 2 files is not advisable and that\u2019s when the Magento 2 Helper class comes in picture to override the core files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The programmatic method to call helper function in phtml in Magento 2 as given below can be helpful to implement custom functionalities and enhance the store features!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Call Helper Function in phtml in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Call directly helper class<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    $helper = $this->helper([Vendor]\\[Module]\\Helper\\Data);\n    $values = $helper->HelperMethod();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Using block class<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php \n\nnamespace [Vendoe]\\[Module]\\Block;\n\nclass BlockName extends \\Magento\\Framework\\View\\Element\\Template\n{\n     protected $helper;\n\n     public function __construct(\n     ....\n    [Vendor]\\[Module]\\Helper\\Data $helperData,\n    ....\n) {\n    ....\n    $this->helper = $helperData;\n    ....\n}\n\npublic function yourMethod()\n{\n    return $this->helper->methodName();\n}\n\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to call block in template file like display.phtml, write $block-&gt;yourMethod(); Here, BlockName class is block that renders the template display.phtml file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You may also refer the programmatic method to <a href=\"https:\/\/meetanshi.com\/blog\/call-cms-static-block-in-phtml-file-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">call CMS static block in pHTML file in Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the post with fellow developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Helper classes offer functionalities for many features in Magento 2 stores. With Magento 2, the Helper can be called in controllers, models, views and other helpers. The below code can be used when you want to have helper\u2019s data in .phtml file in Magento 2. Making changes in the default Magento 2 files is not [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-599","post","type-post","status-publish","format-standard","hentry","category-magento"],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=599"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/599\/revisions"}],"predecessor-version":[{"id":15263,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/599\/revisions\/15263"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}