In Magento 2, I have a custom login form in Header.
If a customer is on the product page for eg :
http://www.testwebsite.com/catalog/product/view/id/10
and login from header form then customer should redirect to the same product page instead of Customer Dashboard.
I have tried plugin code from this link.
Below is my custom header form code –
<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->get('MagentoCustomerModelSession');
$urlInterface = MagentoFrameworkAppObjectManager::getInstance()->get('MagentoFrameworkUrlInterface');
$currenturl = $urlInterface->getCurrentUrl();
if(!$customerSession->isLoggedIn()) {?>
<div class="header-login-main">
<form action="<?php echo $this->getUrl('customer/account/loginPost');?>" method="post" id="login-form" novalidate="novalidate">
<input name="form_key" value="" type="hidden">
<div class="header-login" style="">
<span>Email</span>
<input name="login" id="email" class="input-text" title="Email" data-validate="{required:true, 'validate-email':true}" aria-required="true" type="email">
</div>
<div class="header-login">
<span>Password</span>
<input name="login[password]" id="pass" class="input-text" title="Password" data-validate="{required:true}" aria-required="true" type="password">
</div>
<div class="header-login1">
<button type="submit" class="action login primary" name="send" id="send2"><span>Login</span>
</div>
</form>
<a href="<?php echo $this->getUrl('customer/account/forgotpassword');?>" class="f-right" style="padding: 3px 5px;">Reset Your Password?</a>
</div>
<?php } ?>
Plugin Code –
<?php
namespace VendorModulePlugin;
class LoginPostPlugin
{
/**
* Change redirect after login to home instead of dashboard.
*
* @param MagentoCustomerControllerAccountLoginPost $subject
* @param MagentoFrameworkControllerResultRedirect $result
*/
public function afterExecute(
MagentoCustomerControllerAccountLoginPost $subject,
$result)
{
$result->setPath('/'); // Change this to what you want
return $result;
}
}
I want to redirect the customer to $currenturl. Link tutorial can be able to redirect to the home page. Please suggest what needs to be done to achieve to redirect the customer to the current page.
3
Answers
If you have a plugin that receives the
MagentoCustomerControllerAccountLoginPost
object you could get the referer URL like :$subject->_redirect->getRefererUrl()
. Then return this URLI’m not 100% sure if this URL will actually be the URL you want but worth a try.
Please try with the following code I have set the current url to the instant of $resultRedirect like this : $resultRedirect->setUrl($currenturl);
Try the below:
Header Login Form :-
Plugin Code : –