{"id":11340,"date":"2025-04-03T12:00:00","date_gmt":"2025-04-03T12:00:00","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/?p=11340"},"modified":"2025-04-03T04:08:35","modified_gmt":"2025-04-03T04:08:35","slug":"show-related-products-in-the-popup-modal-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/show-related-products-in-the-popup-modal-in-magento-2\/","title":{"rendered":"How to Show Related Products in the Popup Modal in Magento 2?\u00a0\u00a0"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Capturing customers&#8217; attention while they are in your store is a pure game of smart efforts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One of the ways is displaying related products in a popup.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1154\" height=\"1064\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/04\/Related-Products-in-the-Popup-Modal-in-Magento-2.png\" alt=\"Related Products in the Popup Modal in Magento 2\" class=\"wp-image-11342\" srcset=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/04\/Related-Products-in-the-Popup-Modal-in-Magento-2.png 1154w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/04\/Related-Products-in-the-Popup-Modal-in-Magento-2-250x231.png 250w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/04\/Related-Products-in-the-Popup-Modal-in-Magento-2-700x645.png 700w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/04\/Related-Products-in-the-Popup-Modal-in-Magento-2-768x708.png 768w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/04\/Related-Products-in-the-Popup-Modal-in-Magento-2-403x372.png 403w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/04\/Related-Products-in-the-Popup-Modal-in-Magento-2-964x889.png 964w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/04\/Related-Products-in-the-Popup-Modal-in-Magento-2-120x111.png 120w\" sizes=\"auto, (max-width: 1154px) 100vw, 1154px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">This allows your customers to engage with more products, often resulting in more sales. In this blog, I will show you how to implement this functionality on your Magento 2 store using our solution.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps: Show Related Products in the Popup Modal in Magento 2<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create a Modal Template<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, create a new template file for the modal in your custom theme or module.<br>Create a <strong>related_products.phtml <\/strong>file in a custom module under <strong>app\/code\/Vendor\/Module\/view\/frontend\/templates\/modal and <\/strong>add this code.<\/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$relatedProducts = $block->getRelatedProducts();\n?>\n&lt;div id=\"related-products-modal\" class=\"modal fade\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"relatedProductsModalLabel\" aria-hidden=\"true\">\n    &lt;div class=\"modal-dialog\" role=\"document\">\n        &lt;div class=\"modal-content\">\n            &lt;div class=\"modal-header\">\n                &lt;h5 class=\"modal-title\" id=\"relatedProductsModalLabel\">Related Products&lt;\/h5>\n                &lt;button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\">\n                    &lt;span aria-hidden=\"true\">&amp;times;&lt;\/span>\n                &lt;\/button>\n            &lt;\/div>\n            &lt;div class=\"modal-body\">\n                &lt;?php if (!empty($relatedProducts)): ?>\n                    &lt;ul class=\"related-products-list\">\n                        &lt;?php foreach ($relatedProducts as $product): ?>\n                            &lt;li>\n                                &lt;a href=\"&lt;?= $product->getProductUrl() ?>\">\n                                    &lt;img src=\"&lt;?= $block->getImageUrl($product) ?>\" alt=\"&lt;?= $product->getName() ?>\" \/>\n                                    &lt;span>&lt;?= $product->getName() ?>&lt;\/span>\n                                &lt;\/a>\n                            &lt;\/li>\n                        &lt;?php endforeach; ?>\n                    &lt;\/ul>\n                &lt;?php else: ?>\n                    &lt;p>No related products found.&lt;\/p>\n                &lt;?php endif; ?>\n            &lt;\/div>\n        &lt;\/div>\n    &lt;\/div>\n&lt;\/div>\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create a Block Class<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next, create a block class to fetch the related products for the current product.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a <strong>RelatedProducts.php<\/strong> file at<strong> app\/code\/Vendor\/Module\/Block<\/strong> and use the below code:<\/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 Vendor\\Module\\Block;\n\nuse Magento\\Catalog\\Block\\Product\\AbstractProduct;\nuse Magento\\Catalog\\Model\\ProductFactory;\nuse Magento\\Framework\\Registry;\nuse Magento\\Framework\\View\\Element\\Template\\Context;\n\nclass RelatedProducts extends AbstractProduct\n{\n    protected $productFactory;\n    protected $registry;\n\n    public function __construct(\n        Context $context,\n        ProductFactory $productFactory,\n        Registry $registry,\n        array $data = []\n    ) {\n        $this->productFactory = $productFactory;\n        $this->registry = $registry;\n        parent::__construct($context, $data);\n    }\n\n    public function getRelatedProducts()\n    {\n        $product = $this->registry->registry('current_product');\n        if ($product) {\n            return $product->getRelatedProducts();\n        }\n        return [];\n    }\n\n    public function getImageUrl($product)\n    {\n        $imageHelper = $this->helper('Magento\\Catalog\\Helper\\Image');\n        return $imageHelper->init($product, 'product_base_image')->getUrl();\n    }\n}\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code will help to handle the logic for retrieving related products and providing image URLs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Add Modal Template to the Layout<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This step will help you to include the modal in the product page of the website.<br>Create <strong>catalog_product_view.xml <\/strong>file under <strong>app\/code\/Vendor\/Module\/view\/frontend\/layout <\/strong>directory with the below code:<\/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;?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;body>\n        &lt;referenceContainer name=\"content\">\n            &lt;block class=\"Vendor\\Module\\Block\\RelatedProducts\" name=\"related.products.modal\" template=\"Vendor_Module::modal\/related_products.phtml\" \/>\n        &lt;\/referenceContainer>\n    &lt;\/body>\n&lt;\/page>\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Initialize the Modal Using JavaScript<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add JavaScript to activate the modal functionality and initialize\/handle the modal&#8217;s behavior.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This script should be added to your template file or included as a separate JavaScript file.<br>Create <strong>modal.js <\/strong>file at app\/code\/Vendor\/Module\/view\/frontend\/web\/js directory and use the below code:<\/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;script type=\"text\/x-magento-init\">\n{\n    \"#related-products-modal\": {\n        \"Magento_Ui\/js\/modal\/modal\": {\n            \"type\": \"popup\",\n            \"title\": \"Related Products\",\n            \"buttons\": []\n        }\n    }\n}\n&lt;\/script>\n\n&lt;script type=\"text\/javascript\">\nrequire([\n    'jquery',\n    'Magento_Ui\/js\/modal\/modal'\n], function($, modal) {\n    var options = {\n        type: 'popup',\n        responsive: true,\n        innerScroll: true,\n        title: 'Related Products'\n    };\n\n    var popup = modal(options, $('#related-products-modal'));\n\n    $('#your-trigger-element').on('click', function() {\n        $('#related-products-modal').modal('openModal');\n    });\n});\n&lt;\/script>\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Include JS in to your layout<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add the necessary JavaScript in the layout to ensure the modal operates correctly.\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Modify the<strong> catalog_product_view.xml<\/strong> file as shown:<\/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;?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;body>\n        &lt;referenceContainer name=\"content\">\n            &lt;block class=\"Vendor\\Module\\Block\\RelatedProducts\" name=\"related.products.modal\" template=\"Vendor_Module::modal\/related_products.phtml\" \/>\n        &lt;\/referenceContainer>\n        &lt;referenceContainer name=\"head.additional\">\n            &lt;block class=\"Magento\\Framework\\View\\Element\\Template\" name=\"related.products.modal.js\" template=\"Magento_Theme::js\/related_products_modal.phtml\" \/>\n        &lt;\/referenceContainer>\n    &lt;\/body>\n&lt;\/page>\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code will ensure the modal opens when the trigger (button) is clicked.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Add a button to Trigger the Modal<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To trigger the modal, you\u2019ll need to add a button on the product page that users can click to open the modal.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add the button tag as shown to the <strong>catalog_product_view.phtml<\/strong> file to add the button:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>&lt;button id=&#8221;show-related-products&#8221; type=&#8221;button&#8221; class=&#8221;action primary&#8221;&gt;Show Related Products&lt;\/button&gt;<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After following all the steps, clear the cache and deploy the static content by running the necessary commands to apply changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Adjust the path and namespace according to your module and theme structure.<br>That\u2019s it! Your related products will start displaying as the shoppers scroll through your store. \ud83c\udf89<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Capturing customers&#8217; attention while they are in your store is a pure game of smart efforts. One of the ways is displaying related products in&#8230;<\/p>\n","protected":false},"author":5,"featured_media":11361,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-11340","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/11340","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=11340"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/11340\/revisions"}],"predecessor-version":[{"id":11344,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/11340\/revisions\/11344"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media\/11361"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=11340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=11340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=11340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}