i have a magento site in which whenever i go to some page in that site i am redirected to account/login page so is there any way to store that previous page when i am redirected to login page please help
Hello @miniOrange_inc
To redirect the Magento 2 login page (account/login) back to the previous page after a successful login,
Here’s a step-by-step approach:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="customer_login"> <observer name="redirect_after_login" instance="Vendor\Custom\Observer\RedirectAfterLogin"/> </event> </config>
namespace Vendor\Custom\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\App\Response\RedirectInterface; use Magento\Framework\App\RequestInterface; class RedirectAfterLogin implements ObserverInterface { protected $redirect; protected $request; public function __construct( RedirectInterface $redirect, RequestInterface $request ) { $this->redirect = $redirect; $this->request = $request; } public function execute(Observer $observer) { $redirectUrl = $this->request->getParam('referer'); if ($redirectUrl) { $this->redirect->redirect($this->request->getControllerAction()->getResponse(), $redirectUrl); } else { $this->redirect->redirect($this->request->getControllerAction()->getResponse(), '/'); } } }
php bin/magento module:enable Vendor_RedirectLogin php bin/magento setup:upgrade php bin/magento cache:clean php bin/magento cache:flush
Hope it helps !
If you find our reply helpful, please give us kudos.
A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.
WebDesk Solution Support Team
Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Thank You,
WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Location: 150 King St. W. Toronto, ON M5H 1J9