Displaying Magento Static Block as a Pop-up on Page Load
Hello developers! Today we’re going to tackle an interesting challenge: displaying a static block in Magento as a pop-up window upon page load. This is particularly useful for one-time promotions or subscription forms.
What You’ll Learn
By the end of this tutorial, you will:
- Add Lightbox CSS and JavaScript files.
- Create a link to your desired static block.
- Configure cookies for expiration.
- Solve common issues like calling a static block in a pop-up.
Prerequisites
Before we dive in, ensure the following:
- You are running Magento 1.7 or later.
- Your site is a fresh copy with no prior modifications to core files.
- You have access to upload and modify template files via SFTP/SSH.
Step-by-Step Guide
Add Lightbox CSS & JavaScript Files
First, let’s incorporate the Lightbox library into your theme. For this example, we’ll use the default Magento theme (rwd).
-
Navigate to
app/design/frontend/rwd/default/layout/local.xml. -
Inside the
<layout>tag, add the following:
<default>
<reference name="head">
<action method="addJs"><file>lightbox/lightbox.js</file></action>
<action method="addCss"><file>lightbox/lightbox.css</file></action>
</reference>
</default>
- Save and close the file.
Create a Link to Your Static Block
Next, we need to create a link in your template that will trigger the pop-up with our static block content.
-
Open
app/design/frontend/rwd/default/template/page/html/header.phtml. -
Inside the
<body>tag, add the following line:
<a href="<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('newsletter_subscribe')->toHtml(); ?>" rel="lightbox">Subscribe to Our Newsletter</a>
Replace 'newsletter_subscribe' with your actual block identifier.
Configure Cookies for Expiration
To ensure the pop-up appears only once and within a specific time frame, we’ll configure cookies. Magento handles this natively via the core/session configuration node.
-
Open
app/etc/local.xml. -
Locate
<config><global>. -
Inside
<session_save>(or add it if not present), set the cookie lifetime:
<session_save>
<cookie_lifetime>604800</cookie_lifetime> <!-- 1 week in seconds -->
</session_save>
Resolving Common Issues
Calling a Static Block in a Pop-up
The issue you faced likely stems from not setting the rel attribute correctly. Ensure your link looks like this:
<a href="<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('newsletter_subscribe')->toHtml(); ?>" rel="lightbox">Subscribe to Our Newsletter</a>
Lightbox isn’t Working
Double-check the following:
- Ensure you have uploaded both
lightbox.jsandlightbox.cssinto your theme’s JavaScript folder. - Verify your template (.phtml) file is in the correct location.
- Confirm there are no syntax errors or missing semicolons.
Conclusion
That’s it! You now know how to display a Magento static block as a pop-up window on page load, with cookies expiring after a specified period. This technique can be applied across various themes and blocks for different purposes.
Remember to always test changes in a development environment before deploying them live.
If you found this tutorial helpful, please let us know! We’d love to hear your feedback. Additionally, check out our other Magento tutorials at Laravel Company.
Happy coding!