How to Redirect Customer to Custom Page after Login in Magento 2
The default Magento 2 redirects a customer to their “My Account” dashboard section or the previous page once they log in to the store.
However, not all the default functionalities may be suitable for your modern online store. Fortunately, the Magento CMS is flexible enough to allow the developers to tweak the features and customize them based on the business requirements.
In today’s post, I’ll give the solution for one such similar customization that store owners frequently require.
When a customer logs in to your store, it is the prime time when you can redirect their attention to things you want. It is a short time span where you can lure them for engagement or even conversion.
So why not redirect them to your latest products, or best sellers or even some offers and discounts that you may be offering in your store at once when they log in!
You can redirect your customers to some appointment page if you have one and your business requires customers to book an appointment. Or, you can simply redirect them to any custom CMS page that you want them to have a look at.
It can be done using the below solution to redirect customer to custom page after login in Magento 2.
Method to Redirect Customer to Custom Page after Login in Magento 2
- Use below code in di.xml file at Vendor/Extension/etc/frontend
1234567<?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\Account\LoginPost"><plugin name="redirect_custom_url" type="\Vendor\Extension\Plugin\RedirectCustomUrl" sortOrder="1"/></type></config> - Paste the below code in RedirectCustomUrl.php file at Vendor/Extension/Plugin
1234567891011121314151617<?phpnamespace Vendor\Extension\Plugin;class RedirectCustomUrl{public function afterExecute(\Magento\Customer\Controller\Account\LoginPost $subject,$result){$customUrl = 'sales/order/history';$result->setPath($customUrl);return $result;}}
Use the path of your required page instead of ‘sales/order/history’ in $customUrl variable.
Done!
If you have any doubt regarding the above solution, do mention them in the Comments section below.
I would be happy to help.
Feel free to share the method with Magento Community via social media.
Thank You.
Related Posts:
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
6 Comments
I want to use this code only if user login from cart page when guest checkout is disabled. Currently when guest checkout is disabled the user login from cart page and stay on cart page and again customer have to click one more time to go to the checkout page.
Hii Ashutosh , For that you need to make changes in the
cart page login form and even in controller.
Thank You!!
How to call this codes in the CMS page ?
Hello Mong,
Please be informed that plugin code can not be called on the CMS page directly.
However, if you want to call this code on the CMS page then you first need to add the redirect customer code into the phtml file and then you need to call this phtml file in the CMS page.
Thank You
How to redirect to the previous product page after login?
In my case ‘Redirect Customer to Account Dashboard after Logging in’ = No setting is not working.
Hello Vijesh,
It will redirect to custom page using above code.
If you’ll not use the above code, it will work as default Magento’s functionality!
Thank You