How to Redirect Customer Login or Registration Success to Checkout in Magento 2
Magento 2, by default, offers many features that help store owners to increase conversions. However, sometimes the default Magento 2 lacks functions that might just be helpful for that little push required for customers to convert!
To overcome such limitations, developers can tweak the default features to enhance the user experience, one which I discussed earlier to change the default configuration to redirect customers to the previous page after login in Magento and Magento 2.
Similarly, I have implemented a solution to redirect customer login or registration success to checkout in Magento 2. However, you’ll have to follow the programmatic method to do so.
Also you can redirect customer to custom page after login in Magento 2 as 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.
With this solution, the guest visitors who have selected items and added them to cart won’t have to repeat the process after logging in or registering in your store! You can simply take them to the checkout step and encourage them to buy the items already added to the cart.
Steps to Redirect Customer Login or Registration Success to Checkout in Magento 2:
- Create di.xml file at app/code/Vendor/Extension/etc/ folder
123456<?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\Checkout\Model\Session"><plugin name="set_redirection_url_if_have_quote" type="Vendor\Extension\Plugin\LoginRedirection" /></type></config> - Create LoginRedirection file at app/code/Vendor/Extension/Plugin/ folder
12345678910111213141516171819202122232425262728<?phpnamespace Vendor\Extension\Plugin;class LoginRedirection{protected $storeManager;protected $customerSession;public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager,\Magento\Customer\Model\Session $customerSession){$this->storeManager = $storeManager;$this->customerSession = $customerSession;}public function afterLoadCustomerQuote(\Magento\Checkout\Model\Session $subject,$result){$quote = $subject->getQuote();if(count($quote->getAllItems())>0){$this->customerSession->setBeforeAuthUrl($this->storeManager->getStore()->getUrl('onepagecheckout/index/index'));}}}
That’s it.
Take your newly signed up customers directly to checkout, prompt them to make a purchase, and enjoy sales!
Do mention any doubts in the Comments section below. I’d be happy to help!
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
main.ERROR: Plugin class Vendor\Extension\Plugin\LoginRedirection doesn’t exist
Hi sachin,
You have forgotten to define the class. simply define the same class in your extension and change the path accordingly.
Hello, thanks for the code. But mine is not working. I am using magento 2.3.2 installed via composer. I installed it in the following folders. / app / code / Mage2redirect / LoginRedirection / etc (di.xml) and app / code / Mage2redirect / LoginRedirection / Plugin (LoginRedirection.php) I was confused if I did it right, because it didn’t work in my e-commerce.
Hello Roni,
Please enable the error log and check if the file is called or not.
Thanks.
If I have an account on the store and I add a product to my cart as a guest and try to login, the redirect to checkout works perfectly. But, if I log out from the store and I login again (from any other page, like homepage) I’ll be redirected to the checkout again instead of “My account”. According to a “normal” e-commerce flow I should be redirected to “My account”.
Thanks you for your code, it’s a great starting point!
Hello,
The above code facilitates the logged-in user to get redirected directly to the checkout if the product is added to the cart.
And, thanks for the appreciation. Happy to help 🙂