Displaying Login/Register Forms & Discounts in Lightbox on Magento Homepage
As a Magento developer, I often receive requests from clients to enhance the user experience by displaying login forms, registration forms, or special offers directly on the homepage via lightboxes. This allows users to quickly access these features without leaving the current page.
In this blog post, we’ll guide you through implementing this functionality for your Magento 1.6.1 store. We’ll use a combination of custom PHP code, phtml templates, and JavaScript to achieve the desired behavior.
Prerequisites
Before we begin, ensure that you have:
- A good understanding of basic PHP and HTML
- Familiarity with the Magento admin panel
- Access to your server via SSH or an FTP client
Let’s start by creating a new module to hold our custom code. Navigate to the root directory of your Magento installation using your preferred method (SSH, FTP).
Step 1: Create a New Module
Create a new folder in the app/code/local directory with the following structure:
app/code/local/
âââ MyCompany/
âââ LightboxLogin/
âââ etc/
â âââ config.xml
â âââ system.xml
âââ Block/
âââ Login.php
âââ Register.php
The config.xml and system.xml files will define the module’s configuration, while the Login and Register PHP classes will handle the form rendering.
Step 2: Define the Module Configuration
Open the app/code/local/MyCompany/LightboxLogin/etc/config.xml file and add the following content:
<?xml version="1.0"?>
<config>
<modules>
<MyCompany_LightboxLogin>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Core />
<Mage_Page />
</depends>
</MyCompany_LightboxLogin>
</modules>
<global>
<blocks>
<mycompany_lightboxlogin>
<class>MyCompany_LightboxLogin_Block</class>
</mycompany_lightboxlogin>
</blocks>
</global>
</config>
This configuration file tells Magento that our module is active and defines the class path for our custom blocks.
Step 3: Create Custom Blocks
Now, let’s create the PHP classes for our login and registration forms.
Login Form Block (Login.php)
In the app/code/local/MyCompany/LightboxLogin/Block/Login.php file, add the following code:
<?php
class MyCompany_LightboxLogin_Block_Login extends Mage_Core_Block_Template
{
public function _toHtml()
{
$form = new Varien_Data_Form();
$form->setAction($this->getUrl('customer/account/loginPost'))
->setId('loginform')
->setMethod('post');
$form->setUseContainer(true);
$form->addElement(
'text',
'username',
[
'label' => $this->__('Username'),
'class' => 'input-text required-entry validate-email',
'name' => 'login[username]'
]
);
// Add more form fields as needed (e.g., password, remember me)
return parent::_toHtml();
}
}
This block extends the Mage_Core_Block_Template class and defines a simple login form. You can customize this form by adding additional elements like ‘password’ and ‘remember me’ checkbox.
Register Form Block (Register.php)
Create a similar block for the registration form in the app/code/local/MyCompany/LightboxLogin/Block/Register.php file:
<?php
class MyCompany_LightboxLogin_Block_Register extends Mage_Core_Block_Template
{
public function _toHtml()
{
$form = new Varien_Data_Form();
$form->setAction($this->getUrl('customer/account/createpost'))
->setId('createaccountform')
->setMethod('post');
// Add registration form fields here...
return parent::_toHtml();
}
}
Step 4: Define the Lightbox Functionality
To display our forms in a lightbox, we’ll use some JavaScript code along with Magento’s default CMS page editing functionality.
Customize the CMS Landing Page
-
Log in to your Magento admin panel and navigate to
CMS > Pages. -
Find or create a new page that will serve as the homepage for this feature (e.g., “Login/Register in Lightbox”).
-
Click on ‘Edit’ for this page.
-
In the content field, add the following code:
<div id="login-link" class="mycompany-lightbox-anchor">Login</div>
<div id="register-link" class="mycompany-lightbox-anchor">Register</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#login-link').click(function() {
$.lightbox({
overlay: true,
content: '<iframe src="' + '<?php echo $this->getUrl('mycompany_lightboxlogin/login') ?>' + '" width="500" height="300"></iframe>'
});
});
$('#register-link').click(function() {
$.lightbox({
overlay: true,
content: '<iframe src="' + '<?php echo $this->getUrl('mycompany_lightboxlogin/register') ?>' + '" width="500" height="300"></iframe>'
});
});
});
</script>
This code creates two anchor tags for “Login” and “Register” that trigger the lightbox when clicked. The content of the lightbox is an iframe pointing to the URL generated by our custom blocks.
Step 5: Create the Lightbox Templates
Create a new file app/design/frontend/default/default/template/mycompany.lightboxlogin/lightbox.phtml with the following content:
<div id="lightbox">
<div class="lb-overlay"></div>
<div class="lb-container">
<button type="button" class="lb-close">Ã</button>
<!-- The iframe will be inserted here by JavaScript -->
</div>
</div>
This template defines the basic structure for our lightbox overlay.
Step 6: Customize the Form Templates
Create a new file app/design/frontend/default/default/template/mycompany.lightboxlogin/login-form.phtml (and a similar one for the registration form) with the following content:
<div class="form-container">
<?php echo $this->getChildHtml('login_form'); ?>
</div>
This template will include our custom login block created earlier.
Step 7: Compile and Enable the Module
-
In your Magento admin panel, navigate to
System > Configuration. -
Click on ‘Advanced’ in the left sidebar menu.
-
Select ‘Compiler Options’.
-
Set ‘Enable Compiler’ to ‘Yes’. This will generate files for our module.
-
Click on ‘Save Config’.
-
To enable our module, run the following command via SSH or cron:
php shell/compiler.php compile
Conclusion
By following these steps, you’ve created a custom Magento module that displays login and registration forms in a lightbox directly on your homepage. This enhances user experience by reducing clicks and improving accessibility to essential account features.
For more advanced customizations, feel free to explore the official Magento 1.x Developer’s Guide or contact our dedicated Magento support team at Laravel Company for expert assistance.
Happy coding!