Categories: How ToMagento 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" >

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.

Click to rate this post!
[Total: 15 Average: 3.9]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • 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)?

  • 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!

  • 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.

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

6 days ago