In a mutistore-multiwebsite Magento, I'd like to force users to login to the same website they have registered. They might use any login form, in any website, but the form should check their credentials and redirect them to the correct website.
I tried to check the customer's website, and force him to login. It's not working quite good though. The user gets logged in to the current website, not the one he's registered.
In app/code/local/mage/Customer/Session.php
public function login($username, $password)
{
/**************************************************/
$customer = Mage::getModel("customer/customer");
$customer_website = null;
foreach (Mage::app()->getWebsites() as $website) {
$customer->setWebsiteId($website->getId());
$customer->loadByEmail($username);
//check if user exists
if($customer->getName()){
$customer_website = $website->getId();
}
}
/*************************************************/
$customer = Mage::getModel('customer/customer')->setWebsiteId($customer_website);
if ($customer->authenticate($username, $password)) {
$this->setCustomerAsLoggedIn($customer);
return true;
}
return false;
}
Any ideas?
Best Answer
First,you need to do some changes in setting.
Share customer accounts between multiple Website
You should configure this feature here:
System -> Configuration -> Customer Configuration -> Share Customer Accounts
.Make this setting to Global for sharing every customer to all websites.
Share login between websites
To keep the session when switching between stores on different website, enable "Use SID on Frontend" in System > Configuration > General > Web:
Force users to redirect to the same website they have registered
Forcefully customer login to the same website they have registered when we have tried to login from another website.
Use
customer_login
Define event to config.xml
Observer class:
NOTE:
I HAVE TEST THIS CODE on MY MAGENTO DEMO STORE WEBSITES.
This two website is running from same magento instance using website concept .