Issue
I have created 2 store views for my magento website running under magento 1.9.2, I need to use the same domain name for both of the websites. When a customer logged in, I need to change the store view to the second one. I need to check the customer session in the index.php file in root directory. But when I follow the below changes to index.php, I end up with an error
$store_id = 1;
//Check customer session here
if(Mage::getSingleton('customer/session')->isLoggedIn()){ // Error occurs here
$customer = Mage::getModel('customer/session')->getCustomer();
$customerId = $customer->getId();
// $storeid get logged in customer storeid may from customer custom attribute value added on admin edit form
// $store_id value get changed here
}
Mage::app()->setCurrentStore($store_id);
umask(0);
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
if($store_id == 2)
$mageRunCode = 'mysecondshop';
Mage::run($mageRunCode, $mageRunType);
When I check the issue, I find that we cannot use Mage::getSingleton before Mage::run, but for my current situation for switching between stores, I need to get logged in user details before mage run. Is there any way to achieve my requirement?
Solution
In magento it is not possible to check session data unless Mage::run is initilaized. We need to do some temporary storage like cookie to achieve this. Hopefully the below steps will make it works.
Create a custom module with observer for saving, clearing and checking cookie. Your config.xml file is given below
<!--------------------------------------------------------------------->
<customer_login>
<observers>
<Company_Modulename_model_customer_login>
<class>Company_Modulename_Model_Observer</class>
<method>afterCustomerLoggedIn</method>
</Company_Modulename_model_customer_login>
</observers>
</customer_login>
<customer_logout>
<observers>
<Company_Modulename_model_customer_logout>
<class>Company_Modulename_Model_Observer</class>
<method>afterCustomerLoggedOut</method>
</Company_Modulename_model_customer_logout>
</observers>
</customer_logout>
<controller_action_layout_load_before>
<observers>
<Company_Modulename_model_layoutload_observer>
<class>Company_Modulename_Model_Observer</class>
<method>beforeLoadLayout</method>
</Company_Modulename_model_layoutload_observer>
</observers>
</controller_action_layout_load_before>
<!--------------------------------------------------------------------->
Observer implementation is given below
public function afterCustomerLoggedIn(Varien_Event_Observer $observer)
{
$customer = $observer->getCustomer();
$cid = $customer->getid();
$webId = $customer->getWebsiteId();
Mage::getModel('core/cookie')->set('_storeuid',$cid, 60*60*24*1); // Create cookie
Mage::getModel('core/cookie')->set('_storeid',$webId, 60*60*24*1); // Create cookie
}
public function afterCustomerLoggedOut(Varien_Event_Observer $observer)
{
$customer = $observer->getCustomer();
$cid = $customer->getid();
$webId = $customer->getWebsiteId();
Mage::getModel('core/cookie')->set('_storeuid','0', 1); // Clear cookie
Mage::getModel('core/cookie')->set('_storeid','0', 1); // Clear cookie
}
Before rendering pages
public function beforeLoadLayout(Varien_Event_Observer $observer)
{
$cookieValue = Mage::getModel('core/cookie')->get('_storeuid');
$baseurl = Mage::getBaseUrl();
if(Mage::getSingleton('customer/session')->isLoggedIn()){ // For more security , if someone change the cookie value
$customer = Mage::getModel('customer/session')->getCustomer();
$webId = $customer->getWebsiteId();
$cid = $customer->getId();
if( $cookieValue != $cid ){
Mage::getModel('core/cookie')->set('_storeuid',$cid, 60*60*24*1);
Mage::getModel('core/cookie')->set('_storeid',$webId, 60*60*24*1);
header("Location: ".$baseurl); exit;
}
}else{
if( (isset($cookieValue)) && ($cookieValue > 0) ){ // Clear cookie if session is cleared
Mage::getModel('core/cookie')->set('_storeuid','0', 1);
Mage::getModel('core/cookie')->set('_storeid','0', 1);
header("Location: ".$baseurl); exit;
}
}
}
And Finally in index.php , you can check the cookie for switching the store value for loggedin customers
$store_id = 1;
if( (isset($_COOKIE['_storeur'])) && ($_COOKIE['_storeur'] > 0) ){
$store_id = $_COOKIE['_storeur'];
}
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
if($store_id == 2 ) $mageRunCode = 'store2';
if($store_id == 3 ) $mageRunCode = 'store3';
//....................
Mage::run($mageRunCode, $mageRunType);
Answered By - Team Techi Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.