Reputation: 1
We are trying to set customer session pragmatically in Magento. We are setting session through setCustomerAsLoggedIn method. But for some customers its keep loading/processing the request and at the end it shows "Gateway Time Out or Connection Time Out" error. Its random issue and not coming for all customers.
Our Magento setup having 3 websites B2B, B2E, B2C and we are only facing this issue for B2B and B2E website. Please review below function and help with possible solutions.
$customer=Mage::getModel('customer/customer')->load($id);
$session = Mage::getSingleton('customer/session');
$session->setCustomerAsLoggedIn($customer);
$this->_redirect();
Upvotes: 0
Views: 833
Reputation: 2500
Instead of direct passing customer object to the session, you should pass as follows :
$session = Mage::getSingleton('customer/session', array('name' => 'frontend'));
$session->login($email, $password);
$session->setCustomerAsLoggedIn($session->getCustomer());
Thus pass customer object from session after the login completes.
Please try and let us know if this works for you.
In case you want to login the users by email only you can also try with following method
$customer = Mage::getModel('customer/customer')->load($customerId);
if ($customer->getWebsiteId()) {
Mage::init($customer->getWebsiteId(), 'website');
$session = Mage::getSingleton('customer/session');
$session->loginById($customerId);
return $session;
}
You need to fetch the customer Id using email first for above to work and also website is the key
Working Code :
Following is the working code that i checked myself on a demo site :
include("app/Mage.php");
Mage::app();
$customer = Mage::getModel("customer/customer");
$customer->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
$customer->loadByEmail('[email protected]');
$customerId = $customer->getId();
Mage::init($customer->getWebsiteId(), 'website');
$session = Mage::getSingleton('customer/session');
$session->loginById($customerId);
return $session;
exit;
Upvotes: 0