0

I want to redirect customer after login to previous page rather than account dashboard page.

I set the Below Configuration

Login to admin panel Go to System -> Configuration -> CUSTOMERS -> Customer Configuration -> Login Options Set: Redirect Customers to Account Dashboard after Loggin in = No

Below is the code of core controller (did not modify any core class):

/app/code/core/Mage/Customer/controllers/AccountController.php

protected function _loginPostRedirect()
{
    $session = $this->_getSession();

    if (!$session->getBeforeAuthUrl() || $session->getBeforeAuthUrl() == Mage::getBaseUrl()) {
        // Set default URL to redirect customer to
        $session->setBeforeAuthUrl($this->_getHelper('customer')->getAccountUrl());
        // Redirect customer to the last page visited after logging in
        if ($session->isLoggedIn()) {
            if (!Mage::getStoreConfigFlag(
                Mage_Customer_Helper_Data::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD
            )) {
                $referer = $this->getRequest()->getParam(Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME);
                if ($referer) {
                    // Rebuild referer URL to handle the case when SID was changed
                    $referer = $this->_getModel('core/url')
                        ->getRebuiltUrl( $this->_getHelper('core')->urlDecodeAndEscape($referer));
                    if ($this->_isUrlInternal($referer)) {
                        $session->setBeforeAuthUrl($referer);
                    }
                }
            } else if ($session->getAfterAuthUrl()) {
                $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
            }
        } else {
            $session->setBeforeAuthUrl( $this->_getHelper('customer')->getLoginUrl());
        }
    } else if ($session->getBeforeAuthUrl() ==  $this->_getHelper('customer')->getLogoutUrl()) {
        $session->setBeforeAuthUrl( $this->_getHelper('customer')->getDashboardUrl());
    } else {
        if (!$session->getAfterAuthUrl()) {
            $session->setAfterAuthUrl($session->getBeforeAuthUrl());
        }
        if ($session->isLoggedIn()) {
            $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
        }
    }
    $this->_redirectUrl($session->getBeforeAuthUrl(true));
}

Please let me know how can i achieve this in best way.c

site url is medikabazaar.com

  • what is your question? – Sandipan S Jun 8 '18 at 6:15
  • Hi .. i have added your answer, please check it and also please edit your question. – Sandipan S Jun 8 '18 at 6:26
  • I'm voting to close this question because you did not give enough details to find out why/how this is not working. – 7ochem Jun 8 '18 at 7:45
1

I am guessing that you need to redirect your customer to previous page rather than dashboard page.

For this the setting is correct. But there is some more work for this to do. Please check the controller class _loginPostRedirect method you mentioned, check the if ($referer) { section. That is the point you need to go there.

So for this you dont need to change anything in any class. Just add below codes in you login form phtml within <form> tag.

<?php if (!Mage::getStoreConfigFlag(
        Mage_Customer_Helper_Data::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD
    )) { ?>
    <input name="referer" type="hidden" value="<?php echo Mage::helper('core')->urlEncode(Mage::getBaseUrl()); ?>" />
<?php } ?>

Example of a login form,

<form action="<?php echo $this->getPostActionUrl() ?>" method="post">
<input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey(); ?>" />
<?php if (!Mage::getStoreConfigFlag(
        Mage_Customer_Helper_Data::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD
    )) { ?>
    <input name="referer" type="hidden" value="<?php echo Mage::helper('core')->urlEncode(Mage::getBaseUrl()); ?>" />
<?php } ?>
<div class="block-content">
    <label for="mini-login"><?php echo $this->__('Email address:') ?></label><input type="text" name="login[username]" id="mini-login" class="input-text" />
    <label for="mini-password"><?php echo $this->__('Password:') ?></label><input type="password" name="login[password]" id="mini-password" class="input-text" />
    <div class="mini-actions">
        <a href="<?php echo $this->getForgotPasswordUrl() ?>" class="f-left"><?php echo $this->__('Forgot Your Password?') ?></a>
        <button type="submit" class="button"><span><span><?php echo $this->__('Log in') ?></span></span></button>
    </div>
</div>

In above example the customer will goes to the value of the referer field : <input name="referer" type="hidden" value="<?php echo Mage::helper('core')->urlEncode(Mage::getBaseUrl()); ?>" />

So if need to change this value to the current page URL or Previous page so the user comes back to this page. Please change this a per your requirement Mage::getBaseUrl()

Say if your are using it on a mini login form like dropdown, you need to use current URL of the page. If you are in login page, in that case you need to use the previous url ie. HTTP_REFERER ie Mage::helper('core/http')->getHttpReferer() of the login page.

<?php if (!Mage::getStoreConfigFlag(
        Mage_Customer_Helper_Data::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD
    )) { ?>
    <input name="referer" type="hidden" value="<?php echo Mage::helper('core')->urlEncode(Mage::helper('core/http')->getHttpReferer()); ?>" />
<?php } ?>

Hope it helps!! Please upvote and accept it if helps..!!

  • Thanks for above solution its work now.but I have another issue also after login its going on the page which I select in previous session.How can I fix it. – MINAKSHEE AWARI Jun 8 '18 at 6:38
  • I have edited the answer according to your query, please check the bold section, you need to change the value of the referer. Please upvote and accept if it helps you, as that will help me and others in return. – Sandipan S Jun 8 '18 at 6:46
  • My question is. when I loged in first time Now its redirect on home page rather than previous page.pls check the site medikabazaar.com . – MINAKSHEE AWARI Jun 8 '18 at 6:49
  • Yes, as in my code it is Base URL. You need to change this - Say if your are using it on a mini login form like dropdown, you need to use current URL of the page. If you are in login page, in that case you need to use the previous url ie. HTTP_REFERER of the login page. – Sandipan S Jun 8 '18 at 6:52
  • 1
    Please make a check if that if your referer is login, registration or logout page then use home page. – Sandipan S Jun 8 '18 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.