Modal dialog in Magento

Modal dialog in Magento

July 29, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Modal dialog in Magento

Creating Modal Dialog for Category Newsletter Subscriptions in Magento

Hello Suk,

Welcome to the Magento universe! In this guide, we’ll walk you through creating a modal dialog for category newsletter subscriptions using Magento 1.6.1. By the end of this tutorial, your customers will be required to complete the subscription form before accessing a category page.

Understanding Modal Dialog in Magento

A modal dialog is an interactive window that appears on top of the current web page and requires users to interact with it before they can continue using other parts of the interface. In our case, we want to display this window when a customer tries to access a specific category, prompting them to subscribe to the newsletter.

Step 1: Create Your Subscription Block

First, create your custom block for the subscription form. Navigate to app/design/frontend/[Your_Theme]/layout and open or create a new XML layout file (for example, category_newsletter.xml).

<reference name="category_view">
    <block type="newsletter/subscribe" name="customer.newsletter.subscribe" template="newsletter/subscribe.phtml"/>
</reference>

This code adds your custom block to the category view page. The subscribe.phtml file contains the standard newsletter subscription form.

Step 2: Modify Your Category Controller

Next, modify the category controller to intercept access and display the modal dialog before the customer can view the products.

Open the app/code/core/Mage/Catalog/controllers/CategoryController.php file. Find the _initProductList() method around line 70:

public function _initProductList()
{
    // ... existing code ...

    $this->_productCollection = Mage::getSingleton('catalog/product')
        ->getCollection()
        ->addAttributeToFilter('status', 1)
        ->addFieldToFilter('visibility', array('eq' => '4')) // Make sure visibility is set to "Not visible individually"
        ->setPageSize($this->getProductCount())
        ->setCurPage(1);

    return $this;
}

Modify the filter conditions for the product collection:

$this->_productCollection = Mage::getSingleton('catalog/product')
    ->getCollection()
    ->addAttributeToFilter('status', 1)
    ->addFieldToFilter('visibility', array('neq' => '4')) // Change "Not visible individually" to "Visible individually"
    ->setPageSize($this->getProductCount())
    ->setCurPage(1);

This change ensures that the category is not actually accessible if there are no products in it.

Step 3: Display Your Modal Dialog

To display your custom block as a modal dialog, you’ll need to modify the category view template. Open app/design/frontend/[Your_Theme]/template/catalog/category/view.phtml.

Find where the product listing begins (<div class="category-products">) and replace it with:

<div id="newsletter-modal" style="display: none;">
    <?php echo $this->getBlockHtml('customer.newsletter.subscribe') ?>
</div>

<script type="text/javascript">
    require(['jquery'], function($) {
        $(document).ready(function() {
            $('.category-products').hide();
            $('#newsletter-modal').modal({backdrop: 'static', keyboard: false});
        });
    });
</script>

This code hides the product listing and displays your modal dialog instead. The modal function is part of Bootstrap, which you should have included in your theme.

Step 4: Allow Access After Subscription

Finally, add a listener to allow access to the category once the customer submits the newsletter subscription form.

Open app/design/frontend/[Your_Theme]/template/newsletter/subscribe.phtml. At the end of the script section (before the closing </script> tag), add:

<script type="text/javascript">
    require(['jquery'], function($) {
        $(document).ready(function() {
            $('#newsletter-submit').click(function(e) {
                e.preventDefault();
                if ($('#newsletter-subscribe-form').valid()) {
                    // Submit the form and allow access to the category
                    $('.category-products').show();
                    $('#newsletter-modal').modal('hide');
                }
            });
        });
    });
</script>

This script listens for the submit event on the newsletter subscription form. If the form is valid, it displays the product listing and hides the modal.

Step 5: Test Your Setup

Clear your cache using index.php?controller=adminhtml/cache_flush and visit a category page to test your setup. You should now see the modal dialog prompting for a newsletter subscription before you can access the products.

Remember, this is a basic example to get you started with modal dialogs in Magento. There are advanced methods involving JavaScript and JQuery that can provide more sophisticated user experiences.

For more detailed guides on Magento development, consider checking out our Magento Development Series at LaravelCompany.

We hope this helps! Don’t hesitate to reach out if you have any other questions about Magento or need assistance with your projects.

Magento 1 Catalog Themes Customers

Our network