In some cases we need to change the default Magento behavior so it would redirect a user not to his account page but to some other page after login. To accomplish this we will use the customer_login event and change the address where the customer will be redirected to after login.
Let’s create a simple module consisting of a single model for observer and helper.
app/code/local/Belvg/Loginredirect/Model/Observer/Customer.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public function customerLogin(Varien_Event_Observer $observer) { if (Mage::helper('loginredirect')->isEnabled()) { $_session = $this->_getSession(); $_session->setBeforeAuthUrl(Mage::helper('loginredirect')->getRedirectUrl()); } } /** * @return Mage_Core_Model_Abstract */ protected function _getSession() { return Mage::getSingleton('customer/session'); } |
In the admin part of the website we will indicate where the customer will be redirected to, and will use helper for that
app/code/local/Belvg/Loginredirect/Helper/Data.php
1 2 3 |
… /** * Return path for redirect |
* * @return string */ public function getRedirectUrl() { $_path = (string) $this->_getConfigValue(‘path_redirect’); return Mage::getUrl($_path); } …
And then define the event:
app/code/local/Belvg/Loginredirect/etc/config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
... <events> <customer_login> <observers> <loginredirect> <class>loginredirect/observer_customer</class> <method>customerLogin</method> </loginredirect> </observers> </customer_login> </events> … |
So these are the basic module steps.
I tried this before and set url correctly. Now, Is this necessary to define loginpost action file?
{ app\code\\Namespace\Modulename\controllers\AccountController.php }
HI,
i have to redirect to the befor page url after login.
Can anyone help
Hi Aleksander,
I have been looking for this solution for a long time and this works great on my localhost, but when I try to implement it on my production site, I am still getting directed to the account dashboard. All other settings seem to be the same and cleared cache. Any suggestions for getting to work on the production version?
Thanks,
Thank you for the tip $_session->setBeforeAuthUrl. =)
Thanks for your response Pavel.
I may have broke the functionality of this module by attempting to setBeforeAuthUrl() on the last page viewed, as long as it’s not a customer-related page, within the header.phtml template.
Is there a way to easily change the module to setBeforeAuthUrl() on any page but a customer page and have the user redirected back to it on successful login? I guess I would have to make a Product observer so it gracefully falls back to the admin-setting redirect URL!?
Thanks!
Hello Brett!
Event customer_login is fired in Mage_Customer_Model_Session::setCustomerAsLoggedIn() method. This method is called only after all credentials verifications, so customer with incorrect login data will be redirected to the native magento login page with error message on it.
Hi Aleksander,
Great module! I do have one concern, though. If someone enters incorrect credentials when trying to login, the user is still redirected. How can the redirect be stopped if the login failed?
Thanks again!
Thank you very much
i tried for this from fast one week finally i got solution here….
I follow all the steps, then upload to my magento and nothing happens
Thanks
EssGee,
Sorry for the late response. To be able to redirect customers of a certain group only you need to make the following changes:
1. Add possibility to specify customer group into the system.xml file
2. Add verification of a customer group in the observer
See changes in the github repository.
Hi, I’d have one question.
I have set setBeforeAuthUrl to the base url of another store/storeview. The customer gets redirected to the correct url, but is not logged in in the store he was redirected to, but is logged in in the store he used the login form.
I wanted to achieve that customers of a special customer group get redirected to one specific store, no matter in what store they log in.
Account sharing is global.
Best regards
Thanks for the question, look at the post How To Redirect Users To Another Page After Login In Prestashop
Hi Aleksander Tretjak,
The above steps for redirecting a user to other page than his account page after login in magento are great.
Can we done same thing in pretashop? If you know please suggest me how?
hatim,
What is the point of creating a cart that depends on an entry point for every single customer?
The cart is tied to a user ID by default.
Hi Aleksander, Have you come across a solution (on the community version) for having one login id and password but accessible from multiple PC’s or locations. What we are discovering is that the cart is duplicating the items from other browesers/sessions when the same User is sgined on from multiple PC’s, Browsers or different locations.
We require this facility as a customer may have 30 users who are accessing the site with the same user name and password. This is unfortunately is required and cannot be altered.
Would appreciate any help you can offer.
Hatim
we have a customer who may have upto 1000
MagePsycho,
Thanks a lot for your comment.
The best way to change or enhance functionality is to use events. That is why we use the event and not expand the controller.
Hi Aleksander,
Thanks for sharing your tips.
Alternatively, you can extend the Mage_Customer_AccountController::_loginPostRedirect() method for the purpose.
Thanks
Regards