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?
Hi @mikemm,
I'm not sure why you should prefer to redirect the custom from the checkout but maybe you can use an observer to be executed after customer login.
At that moment you can redirect to the dashboard as you want.
You can do it by plugin way,
create simple module,
app/code/Rbj/Test/etc/frontend/di.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Customer\Controller\Ajax\Login"> <plugin name="customer-login-after" type="Rbj\Test\Plugin\Afterlogin"/> </type> </config>
Create plugin file,
app/code/Rbj/Test/Plugin/Afterlogin.php
<?php namespace Rbj\Test\Plugin; class Afterlogin { public function __construct( \Magento\Framework\UrlInterface $url, \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory ) { $this->url = $url; $this->resultJsonFactory = $resultJsonFactory; } public function afterExecute(\Magento\Customer\Controller\Ajax\Login $subject) { $response['redirectUrl'] = $this->url->getUrl('customer/account/index'); $resultJson = $this->resultJsonFactory->create(); return $resultJson->setData($response); } }
Now override core magento_customer js file,
vendor/magento/module-customer/view/frontend/web/js/action/login.js to your module and keep below code,
/*global define*/ define( [ 'jquery', 'mage/storage', 'Magento_Ui/js/model/messageList', 'Magento_Customer/js/customer-data' ], function($, storage, globalMessageList, customerData) { 'use strict'; var callbacks = [], action = function(loginData, redirectUrl, isGlobal, messageContainer) { messageContainer = messageContainer || globalMessageList; return storage.post( 'customer/ajax/login', JSON.stringify(loginData), isGlobal ).done(function (response) { if (response.errors) { messageContainer.addErrorMessage(response); callbacks.forEach(function(callback) { callback(loginData); }); } else { callbacks.forEach(function(callback) { callback(loginData); }); customerData.invalidate(['customer']); if (response.redirectUrl) { window.location.href = response.redirectUrl; } else if (redirectUrl) { window.location.href = redirectUrl; } else { location.reload(); } } }).fail(function () { messageContainer.addErrorMessage({'message': 'Could not authenticate. Please try again later'}); callbacks.forEach(function(callback) { callback(loginData); }); }); }; action.registerLoginCallback = function(callback) { callbacks.push(callback); }; return action; } );
if issue soled, Click Kudos/Accept as solutions.
Thank you for the code samples. I've tried them but haven't had any luck. No errors or redirect. Any further thoughts?
this code is working for me. i have try in my local system and its working fine. can you just test from above js file by keeping console.log(response);return false; after customerData.invalidate(['customer']); line,
Run upgrade and deploy command and check. any response getting or not.
Thanks.
@Rakesh Jesadiya : I have also same kind of requirment. like If customer is login from cart page it should redirect to Checkout page.
Any suggestions?
@balwant_singh20 any solution for that?
i have same issue facing when cart page login after stay same page but i want to redirect on checkout page
https://magento.stackexchange.com/q/321465/68695