Create a different Home page for logged In user in Magento
Today we will create a simple plugin to display different home page for logged-In user and guest user without hacking magento core code.
If you donāt have knowledge about plugin structure and want to know then you can go to the following link:
https://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-1-introduction-to-magento
If you feel little lazy then download code directly from my git repo š :
Lets Start the fun stuff now:
Step1:- Create a Module file under the app/etc/modules/Mss_Homepage.xml
<?xml version="1.0"?> <config> <modules> <Mss_Homepage> <active>true</active> <codePool>local</codePool> <version>0.1.0</version> </Mss_Homepage> </modules> </config>
As you know the codePool define the place where you can place our module.
There are three different codepools in magento
1. Code- It contains all magneto default modules provided by core magento team.
2. Community– It contains the plugin provided by community.
3. Local- we always work on the local directory.
If Magento have no local directory then create a local directory. So you can setup your module under the local folder.
app/code/local/Mss/Homepage
Step2 : Now Create the basic folders that are required for our plugin
Step3. Now lets make files that will register the model, helper and system file with the plugin
- app/code/local/Mss/Homepage/etc/config.xml
- app/code/local/Mss/Homepage/etc/system.xml
- app/code/local/Mss/Homepage/etc/adminhtml.xml
config.xml- This file inform’s magento about which block, model, helper and event you are going to use in this plugin.
See Below– In this code, the class node for model āMss_Homepage_Modelā . Magento read this and auto load all model classes starting with this āMss_Homepage_Modelā.
<?xml version="1.0" encoding="UTF-8"?> <config> <modules> <Mss_Homepage> <version>0.1.0</version> </Mss_Homepage> </modules> <global> <helpers> <homepage> <class>Mss_Homepage_Helper</class> </homepage> </helpers> <models> <mss_homepage> <class>Mss_Homepage_Model</class> </mss_homepage> </models> <events> <controller_action_predispatch_cms_index_index> <observers> <mss_homepage_model_observer> <model>mss_homepage/observer</model> <method>homepage</method> </mss_homepage_model_observer> </observers> </controller_action_predispatch_cms_index_index> </events> </global> </config>
System.xml: This file allow you to add your configuration element in magentoās Admin Configuration section with simplest way.
So lets add two configuration element with our module like following image:
adminhtml.xml– This files allow other admin users to access above shown configuration settings.
Step4. Create a helper file under helper folder app/code/local/Mss/Homepage/Helper/Data.php
Basic purpose of adding helper is to save config info because helper provides the common functionality to save the system inputs in magneto. So you don’t have to write any code to save values, You just need to add the fields through the xml file .
So lets add the below class and extends the Core helper class :
<?php class Mss_Homepage_Helper_Data extends Mage_Core_Helper_Abstract { } ?>
Step5. Now lets create Observer file that will be responsible to change the homepage for the login user.
Magento uses the Event/Observer pattern so you can easily plug in your functionality with these events.
To see magento default events click here
https://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/events
app/code/local/Mss/Homepage/Helper/Observer.php
<?php class Mss_Homepage_Model_Observer { const XML_PATH_FOR_CONFIG_TO_HOMEPAGE = 'mss/mss/page'; const XML_PATH_FOR_CONFIG_TO_MSSENABLED = 'mss/mss/enable'; #home page public function homepage() { $HomePageForLoggedIn = Mage::getStoreConfig(self::XML_PATH_FOR_CONFIG_TO_HOMEPAGE); $enabled = Mage::getStoreConfig(self::XML_PATH_FOR_CONFIG_TO_MSSENABLED); if (Mage::helper('customer')->isLoggedIn() && $HomePageForLoggedIn && $enabled) { // Substitute only if page is defined Mage::app()->getStore()->setConfig( Mage_Cms_Helper_Page::XML_PATH_HOME_PAGE, $HomePageForLoggedIn ); } } } ?>
Now you can set the different cms page from the admin config section.
Thats is it my friend!! Now logged-in user should see the different home page š
Where we can use?
Some good projects need to show the different home page for their logged In/ registered user and guest user. So you can use it their and save you time.!!!!!