{"id":7887,"date":"2025-02-06T10:52:24","date_gmt":"2025-02-06T10:52:24","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/?p=7887"},"modified":"2025-09-19T14:55:23","modified_gmt":"2025-09-19T09:25:23","slug":"add-popup-modal-in-hyva-theme","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-popup-modal-in-hyva-theme\/","title":{"rendered":"How to Add a Popup Modal in Magento 2 Hyv\u00e4 Theme?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Are you working on a Hyv\u00e4 theme project and want to add a popup modal? Follow the simple steps provided in this guide to add a popup modal in the Magento 2 Hyv\u00e4 theme.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Popups are visual elements that appear on top of other elements on the screen, forcing users to focus on the provided information. They are used for promoting newsletter signups, discount offers, and announcing important news.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the Magento 2 Hyv\u00e4 theme, we can display such modal popups by creating a custom module. Let&#8217;s understand it more deeply through an example.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Create a Magento 2 Hyv\u00e4 Theme Popup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll create a custom module that displays a custom popup on the Hyv\u00e4 theme.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The custom module structure will look like this:<\/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=\"\">app\/code\/Meetanshi\/HyvaPopup\/\n\u251c\u2500\u2500 etc\/\n\u2502   \u251c\u2500\u2500 module.xml\n\u251c\u2500\u2500 registration.php\n\u251c\u2500\u2500 view\/\n\u2502   \u251c\u2500\u2500 frontend\/\n\u2502   \u2502   \u251c\u2500\u2500 layout\/\n\u2502   \u2502   \u2502   \u251c\u2500\u2500 default.xml\n\u2502   \u2502   \u251c\u2500\u2500 templates\/\n\u2502   \u2502   \u2502   \u251c\u2500\u2500 Template.phtml\n\u2502   \u2502   \u2502   \u251c\u2500\u2500 Popup.phtml<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s go through the steps to create a custom module for showing a custom popup in Hyv\u00e4 theme.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Define &amp; Register a Custom Module<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a registration.php file with the given code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app\/code\/Meetanshi\/HyvaPopup\/registration.php:<\/code><\/pre>\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\\Magento\\Framework\\Component\\ComponentRegistrar::register(\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n    'Meetanshi_HyvaPopup',\n    __DIR__\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, register the module by creating the module.xml file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app\/code\/Meetanshi\/HyvaPopup\/etc\/module.xml:<\/code><\/pre>\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;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Meetanshi_HyvaPopup\" setup_version=\"1.0.0\"\/>\n&lt;\/config><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create a Layout File<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To add the popup modal to Magento 2 Hyv\u00e4 frontend, create the following XML file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app\/code\/Meetanshi\/HyvaPopup\/view\/frontend\/layout\/default.xml:<\/code><\/pre>\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;?xml version=\"1.0\"?>\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n    &lt;update handle=\"hyva_modal\"\/>\n    &lt;body>\n       &lt;referenceBlock name=\"header-content\">\n             &lt;action method=\"setTemplate\">\n                &lt;argument name=\"template\" xsi:type=\"string\">Meetanshi_HyvaPopup::Template.phtml&lt;\/argument>\n            &lt;\/action>\n        &lt;\/referenceBlock>\n    &lt;\/body>\n&lt;\/page><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Create a Popup Template<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now, define a structure for the popup modal in HTML and its trigger.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app\/code\/Meetanshi\/HyvaPopup\/view\/frontend\/templates\/Template.phtml:<\/code><\/pre>\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$modalViewModel = $viewModels->require(\\Hyva\\Theme\\ViewModel\\Modal::class);\n?>\n&lt;div class=\"text-sm\">\n    &lt;div class=\"meetanshi-popup-container\" x-data=\"hyva.modal()\">\n          &lt;?= $escaper->escapeHtml(__('Meetanshi Popup Modal')) ?>\n          &lt;?php\n          $modal = $modalViewModel->createModal()->withTemplate('Meetanshi_HyvaPopup::Popup.phtml');\n          $modalBlock = $modal->getContentRenderer();\n          $modal->withAriaLabel('Meetanshi Popup Modal')\n                ->withDialogRefName('popupRef')\n                ->withDialogClasses('inline-block max-h-screen overflow-auto bg-white shadow-xl rounded-lg text-gray-700 w-3\/4 p-4');\n          ?>\n          &lt;?= \/* @noEscape *\/ $modal ?>\n          &lt;a id=\"popup_button\" href=\"#\" class=\"text-blue-900 font-bold\" @click='show(\"popupRef\", $event)'>\n                 &lt;?= $escaper->escapeHtml(__('Open Meetanshi Popup'))?>\n          &lt;\/a> \n    &lt;\/div>\n&lt;\/div><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The above code uses Hyv\u00e4&#8217;s modal system and displays the popup when the user clicks the &#8220;Open Meetanshi Popup&#8221; button. It loads the popup from the Popup.phtml file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can customize the code and its trigger as per your design requirements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Add Popup Content<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now, we will define the content to be shown in the Hyv\u00e4 theme popup.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app\/code\/Meetanshi\/HyvaPopup\/view\/frontend\/templates\/Popup.phtml:<\/code><\/pre>\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;?= 'Meetanshi Special Offer: Get 20% Off! \ud83c\udf89' ?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once the module is created, run the following commands:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>bin\/magento module:enable Meetanshi_HyvaPopup<\/li>\n\n\n\n<li>bin\/magento setup:upgrade<\/li>\n\n\n\n<li>bin\/magento cache:flush<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Now, test the popup modal on the Magento 2 Hyv\u00e4 front end. You can modify the logic in the template.phtml file to show it dynamically and change its content from the Popup.phtml file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Meetanshi is a well-known Magento and Shopify development company, offering expert Hyv\u00e4 theme development services. Partner with us today to transform your Magento store with a fast-loading Hyv\u00e4 theme.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><div class=\"meetanshi-cta\">\r\n<div class=\"cta-content-wrapper\">\r\n<span> Hyv\u00e4 Development Service<\/span>\r\n<p>Get a fast-loading store in less than 7 weeks with Hyv\u00e4 theme<\/p>\r\n<a href=\"https:\/\/meetanshi.com\/hyva-theme-development-service\" target=\"_blank\" class=\"btn-primary\">Hire Us<\/a>\r\n<\/div>\r\n<div class=\"cta-image-new\">\r\n<img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/11\/hyva-theme-development-service.png\" alt=\"Hyva Theme Development Service\">\r\n<\/div>\r\n<\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you working on a Hyv\u00e4 theme project and want to add a popup modal? Follow the simple steps provided in this guide to add&#8230;<\/p>\n","protected":false},"author":51,"featured_media":7892,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34,5869],"tags":[],"class_list":["post-7887","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento","category-hyva-themes"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/7887","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\/51"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=7887"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/7887\/revisions"}],"predecessor-version":[{"id":22220,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/7887\/revisions\/22220"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media\/7892"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=7887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=7887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=7887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}