How to Redirect to a Particular Page After Login in Magento 2

How to Redirect to a Particular Page After Login in Magento 2

User experience is the key factor to conversion now days and thus store owners never miss a single way to provide it anyhow. Abandon from website causes store owners a great deal of pain. There are many reasons customers decide to leave your web store without spending a single penny, one of them is improper redirection.

Let’s take an example: One of our clients got their Magento store customized to hide product prices by default and show only when they login. Now, at the time of users decide to buy, they are asked to login and redirected to Magento login page. By default, after successful login, users are redirected to their My Account page rather than the page from which the login button is clicked. Boom, you are in danger! You are missing out to provide great shopping experience. You should redirect them to a particular page from which user has tried to login in Magento 2 rather than My account page. 80% of the users will not find the page themselves and definitely choose to abandon. This can costs you missing lots of dollars, right?

Now when you have decided to provide better shopping experience, here I have come up with the code to redirect your customers to a particular page in Magento 2 from which they have clicked login button. Even if you don’t set “No” to “Redirect Customers to Account Dashboard After Logging In” in Magento 2 Stores > Configuration > Customers > Customer Configuration > Login Options, this code will work without any issue.

Implementing the code for this is pretty easy. You just have to add following code in your phtml file where you want to add custom login link or button.

<?php
$url  = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
$login_url = $block->getUrl('customer/account/login', array('referer' => base64_encode($url));
<button type="button" onclick="location.href=' <?php echo $login_url; ?> '"  title="<?php echo "Please Login" ?>" ><span><?php echo "Please Login" ?></span></button>
?>

Hope this guide has helped you to redirect users to the page of your choice and thus provide better shopping experience. Let me know if you are stuck with any issue or query implementing this code. I would be happy to help you any time.

Magento 2 Custome Redirect

Previous Article

Magento Year in Review – 2016

Next Article

How to Add Custom Image Attribute in Magento 2 Categories

Write a Comment
  1. HI i applied your code but it redirect me to the customer/account page not to the page from where user try to click on login url.

  2. Hello,
    Thank you very much for this tutorial. I am trying to add this code to the header of my theme but I am getting a 500 error every time I add it. I am copy/pasting your code as is. Any help would be greatly appreciated.

    Thank you!

  3. Hi,
    Does this code work for the page(For Ex. Page-A) that is not accessible for the guest user and redirected to the account page(for guest) and after login will it be redirected to that page(Page-A)?

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨