将客户重定向到客户登录Magento 2网站的产品页面

在Magento 2中,我在页眉中有一个自定义登录表单。

如果客户在产品页面上例如:

http://www.testwebsite.com/catalog/product/view/id/10

并从标题表格登录,然后客户应重定向到同一产品页面而不是Customer Dashboard。

我已经尝试过此link中的插件代码。

下面是我的自定义标头表单代码-

<?php 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$customerSession = $objectManager->get('Magento\Customer\Model\Session'); 

$urlInterface = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\UrlInterface');
$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[username]" 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 } ?> 

插件代码-

<?php
namespace Vendor\Module\Plugin;
class LoginPostPlugin
{

    /**
     * Change redirect after login to home instead of dashboard.
     *
     * @param \Magento\Customer\Controller\account\LoginPost $subject
     * @param \Magento\Framework\Controller\Result\Redirect $result
     */
    public function afterExecute(
        \Magento\Customer\Controller\account\LoginPost $subject,$result)
    {
        $result->setPath('/'); // Change this to what you want
        return $result;
    }

}

我想将客户重定向到$ currenturl。链接教程可以重定向到主页。请提出要实现的目标,以将客户重定向到当前页面。

gyz111427 回答:将客户重定向到客户登录Magento 2网站的产品页面

请尝试以下代码,我已将当前网址设置为$ resultRedirect的瞬间,如下所示: $ resultRedirect-> setUrl($ currenturl); >

<?php

namespace Vendor\Module\Controller\Account;

use Magento\Customer\Model\Account\Redirect as AccountRedirect;
use Magento\Framework\App\Action\Context;
use Magento\Customer\Model\Session;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Model\Url as CustomerUrl;
use Magento\Framework\Exception\EmailNotConfirmedException;
use Magento\Framework\Exception\AuthenticationException;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Catalog\Api\ProductRepositoryInterface;

class LoginPost extends \Magento\Customer\Controller\Account\LoginPost {

    public function execute() {
        $urlInterface = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\UrlInterface');

        $sku = '24-MB01'; //Edit as per your product sku
        $_product = $this->productRepository->get($sku);
        $currenturl = $_product->getProductUrl();

        if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {
            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirectFactory->create();
            //$resultRedirect->setPath('home');
            $resultRedirect->setUrl($currenturl);
            return $resultRedirect;
        }

        if ($this->getRequest()->isPost()) {
            $login = $this->getRequest()->getPost('login');
            if (!empty($login['username']) && !empty($login['password'])) {
                try {
                    $customer = $this->customerAccountManagement->authenticate($login['username'],$login['password']);
                    $this->session->setCustomerDataAsLoggedIn($customer);
                    $this->session->regenerateId();
                } catch (EmailNotConfirmedException $e) {
                    $value = $this->customerUrl->getEmailConfirmationUrl($login['username']);
                    $message = __(
                            'This account is not confirmed.' .
                            ' <a href="%1">Click here</a> to resend confirmation email.',$value
                    );
                    $this->messageManager->addError($message);
                    $this->session->setUsername($login['username']);
                } catch (AuthenticationException $e) {
                    $message = __('Invalid login or password.');
                    $this->messageManager->addError($message);
                    $this->session->setUsername($login['username']);
                } catch (\Exception $e) {
                    $this->messageManager->addError(__('Invalid login or password.'));
                }
            } else {
                $this->messageManager->addError(__('A login and a password are required.'));
            }
        }

        $resultRedirect = $this->resultRedirectFactory->create();
        //$resultRedirect->setPath('home');
        $resultRedirect->setUrl($currenturl);
        return $resultRedirect;
    }

}
,

请尝试以下操作:

标题登录表单:-

<?php 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$customerSession = $objectManager->get('Magento\Customer\Model\Session'); 

$urlInterface = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\UrlInterface');
$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">
        <input type="hidden" name='referer' value="<?php echo $currenturl ?>">
        <div class="header-login" style="">
            <span>Email</span>
            <input name="login[username]" 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 } ?> 

插件代码:-

<?php
namespace Vendor\Module\Plugin;
class LoginPostPlugin
{
    protected $request;
    public function __construct(
       \Magento\Framework\App\RequestInterface $request
    ) {
       $this->request = $request;
    }
    public function getPostReferer()
    {
        return $this->request->getPostValue('referer');
    }

    /**
     * Change redirect after login to home instead of dashboard.
     *
     * @param \Magento\Customer\Controller\Account\LoginPost $subject
     * @param \Magento\Framework\Controller\Result\Redirect $result
     */
    public function afterExecute(
        \Magento\Customer\Controller\Account\LoginPost $subject,$result)
    {
        $result->setPath($this->getPostReferer());
        return $result;
    }    
}
,

如果您有一个接收Magento\Customer\Controller\Account\LoginPost对象的插件,则可以获取引荐来源网址,例如:$subject->_redirect->getRefererUrl()。然后返回此URL

$productURL = $subject->_url->getUrl($subject->_redirect->getRefererUrl());
return $subject->_redirect($productURL);

我不确定100%是否确实要使用该URL,但值得尝试。

本文链接:https://www.f2er.com/3153634.html

大家都在问