2

I have the setting checked to redirect a customer to their account dashboard after logging in and it works fine on regular pages, but I noticed that it has no effect when someone is on the checkout page and they log in during the checkout processes.

I need to redirect the customer to their dashboard when they are attempting to log in on the shopping cart page during the /checkout/cart/ funnel. Right now, it shows them their shipping options after logging in from the cart and allows them to continue the checkout process. Any solutions?

1
  • Hey Mike! Please accept my answer if it works for you. Jan 8, 2018 at 7:42

1 Answer 1

2

You have to override Ajax login (root_dir\vendor\magento\module-customer\Controller\Ajax\Login.php) controller and login.js(vendor\magento\module-customer\view\frontend\web\js\action\login.js) for ajax login redirect and need to add your conditions accordingly.

Step 1. Create/edit Your_Company/Your_Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Controller\Ajax\Login" type="Your_Company\Your_Module\Controller\Ajax\Login" />
</config>

Step 2. Overridden controller will reside in Your_Company\Your_Module\Controller\Ajax\Login.php Now copy and paste the code from parent Ajax\Login.php in execute method you will see

if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectRoute) {
    $response['redirectUrl'] = $this->_redirect->success($redirectRoute);
    $this->getAccountRedirect()->clearRedirectCookie();
}

Step 3. Now you have to insert your logic below this condition.

$response['redirectUrl'] = $this->_redirect->success($this->_url->getUrl("customer/account"));

This will set the response.redirectUrl for login.js(vendor\magento\module-customer\view\frontend\web\js\action\login.js) and here we got the real issue there is a code snippet you can check

if (redirectUrl) {
    window.location.href = redirectUrl;
} else if (response.redirectUrl) {
    window.location.href = response.redirectUrl;
} else {
    location.reload();
}

You just have to change it to

if (response.redirectUrl) {
    window.location.href = response.redirectUrl;
} else if (redirectUrl) {
    window.location.href = redirectUrl;
} else {
    location.reload();
}

To override login.js follow steps.

Step 4. Create file requirejs-config.js in You_Company\Your_Module\requirejs-config.js and write below code...

var config = {
    map: {
        '*': {
            'Magento_Customer/js/action/login': 'YourCompany_YourModule/js/login'
        }
    }
};

Step 5. Copy login.js from vendor\magento\module-customer\view\frontend\web\js\action\login.js to YourCompany\YourModule\view\frontend\web\js\login.js

That's all.

2
  • 1
    It works for me :) Nov 21, 2017 at 10:26
  • 1
    Thanks it's working Mar 29, 2019 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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