I've set no
on system configuration
-> customer login configuration
. But nothing's happen, it still direct to my account dashboard.
How to redirect to previous page after login?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI've set no
on system configuration
-> customer login configuration
. But nothing's happen, it still direct to my account dashboard.
How to redirect to previous page after login?
You have to rewrite in local loginPostAction()
from app/code/core/Mage/Customer/controllers/AccountController.php
to app/code/local/Customer/controllers/AccountController.php
Then you put this :
public function loginPostAction(){
if (!$this->_validateFormKey()) {
$this->_redirect('*/*/');
return;
}
if ($this->_getSession()->isLoggedIn()) {
$this->_redirect('wlc_business');
return;
}
$session = $this->_getSession();
if ($this->getRequest()->isPost()) {
$login = $this->getRequest()->getPost('login');
if (!empty($login['username']) && !empty($login['password'])) {
try {
$session->login($login['username'], $login['password']);
if ($session->getCustomer()->getIsJustConfirmed()) {
$this->_welcomeCustomer($session->getCustomer(), true);
}
} catch (Mage_Core_Exception $e) {
switch ($e->getCode()) {
case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
$value = $this->_getHelper('customer')->getEmailConfirmationUrl($login['username']);
$message = $this->_getHelper('customer')->__('This account is not confirmed. <a href="%s">Click here</a> to resend confirmation email.', $value);
break;
case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
$message = $e->getMessage();
break;
default:
$message = $e->getMessage();
}
$session->addError($message);
$session->setUsername($login['username']);
} catch (Exception $e) {
// Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
}
} else {
$session->addError($this->__('Login and password are required.'));
}
}
// The adding code here
$this->_redirectReferer(); //redirect you to the previous page
$this->_redirect('/'); // redirect you to the home page
$this->_redirect('wishlist'); //redirect you to wishlist page for exemple
}
$this->getMessagesBlock()->getGroupedHtml();
or the controller if you had modified it, please compare with my controller.
You can customer_login observer. After customer has logged in, you can redirect the customer where you want.
Here is how to create observer for customer login How to bind an action after customer login success using Magento?
Set NO on System > Configuration > Customers > Customer Configuration > Login Options
Then in your login.phtml files (both, persistent/customer/form and customer/form on your template folder), change the action of your <form>
to:
action="<?php echo str_replace('login', 'loginPost', $this->helper('customer')->getLoginUrl()) ?>"
But remember, DO NOT EDIT THE CORE! Create a custom extension to accomplish this job.
Reference: https://stackoverflow.com/a/16099279/5810265
Please use below code to back to prev page after login
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$url = $objectManager->create('\Magento\Framework\UrlInterface');
$loginUrl = $url->getUrl('customer/account/login', array('referer' => base64_encode($url->getCurrentUrl())));
?>
<a href="<?php echo $loginUrl;?>">Login</a>