Magento 2 After login redirect user to referrer
-
08-10-2020 - |
Question
In my magento instance guest checkout is enabled and configuration is set as follows:
System > Config > Customer > Redirect Customer to Account Dashboard after Logging in > NO
I want to redirect user to page he is requesting. Like if user has added some products to cart without logging in and if he clicks on go to checkout button, a login popup displays. Now when he logs in, he should be redirected to checkout page. Basically he should be redirected to the page based on which button is clicked (Go to checkout or View Cart)
Solution
You said guest checkout is enabled in your system but authentication pop up doesn't appear when you have guest checkout enabled. Go to Stores->Configuration->Sales->Checkout. Under Checkout options make sure Guest checkout is set to yes. Authentication pop up can also appear if your cart has a digital product.
Now to answer your question as to how to redirect customer where he intended to go: Overide Magento_Customer/js/view/authentication-popup.js
with your theme or custom module. Line that will interest you is
if ($(loginForm).validation() &&
$(loginForm).validation('isValid')
) {
this.isLoading(true);
loginAction(loginData, null, false);
}
Pass your redirect url instead of null to loginAction function.
You will find implementation of loginAction in file vendor/magento/module-customer/view/frontend/web/js/action/login.js
.
You can decide your redirect url based on which button was clicked. Hope that helps.