Quale classe dovrei ignorare per reindirizzare un cliente a una pagina specifica dopo il login?
Ho provato a impostare Redirect Customer to Account Dashboard after Logging innella configurazione del negozio ma non funziona.
Quale classe dovrei ignorare per reindirizzare un cliente a una pagina specifica dopo il login?
Ho provato a impostare Redirect Customer to Account Dashboard after Logging innella configurazione del negozio ma non funziona.
Risposte:
Un plugin è una soluzione migliore in questo caso perché potrebbe essere necessario aggiornare la classe estesa quando si aggiorna Magento 2.
Ecco una soluzione che utilizza un after-plugin su LoginPost-> execute () come suggerito da Xenocide8998.
/Vendor/Module/etc/frontend/di.xml:
<?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="vendor_module_loginpostplugin" type="\Vendor\Module\Plugin\LoginPostPlugin" sortOrder="1" />
</type>
</config>
/Vendor/Module/Plugin/LoginPostPlugin.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;
}
}
L'ho risolto sovrascrivendo la classe LoginPost
etc / di.xml
<preference for="Magento\Customer\Controller\Account\LoginPost" type="Vendor\Module\Controller\Account\LoginPost" />
Venditore / modulo / Controller / account / LoginPost.php
<?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;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class LoginPost extends \Magento\Customer\Controller\Account\LoginPost {
public function execute() {
if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('home');
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');
return $resultRedirect;
}
}
afterExecute()opzione sarebbe più pulita
Che l'attuale memoria locale ha causato il nostro problema.
Se abilitiamo o disabilitiamo Redirect Customer to Account Dashboard after Logging ine Guest Checkout in Configuration, questa funzione funzionerà bene. Tuttavia, dobbiamo svuotare la memoria locale.
Siamo in grado di controllare l'archiviazione locale localStorage.getItem('mage-cache-storage').
Guarda:
vendor / magento / module-out / view / frontend / web / js / sidebar.js
var cart = customerData.get('cart'),
customer = customerData.get('customer');
if (!customer().firstname && cart().isGuestCheckoutAllowed === false) {
// set URL for redirect on successful login/registration. It's postprocessed on backend.
$.cookie('login_redirect', this.options.url.checkout);
if (this.options.url.isRedirectRequired) {
location.href = this.options.url.loginUrl;
} else {
authenticationPopup.showModal();
}
return false;
}
Magento imposterà il cookie in $.cookie('login_redirect', this.options.url.checkout)base alla customerDatamemoria locale.
Dal controller vendor/magento/module-customer/Controller/Account/LoginPost.php. Controllerà l'URL di reindirizzamento dal cookie.
$redirectUrl = $this->accountRedirect->getRedirectCookie();
if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectUrl) {
......
return $resultRedirect;
}
Versione Magento:
-Magento versione 2.1.0
L'ho risolto passando referer nel controller del modulo personalizzato.
Step1 `
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Customer\Model\Session;
use Magento\Framework\UrlInterface;
class Approve extends \Magento\Framework\App\Action\Action {
/**
* @var \Magento\Framework\View\Result\Page
*/
protected $resultPageFactory;
/**
* $param \Magento\Framework\App\Action\Context $context */
/**
* @param CustomerSession
*/
protected $_customerSession;
protected $_urlInterface;
public function __construct(
Context $context,
PageFactory $resultPageFactory,
Session $customerSession,
UrlInterface $urlInterface
)
{
$this->resultPageFactory = $resultPageFactory;
$this->_customerSession = $customerSession;
$this->_urlInterface = $urlInterface;
parent::__construct($context);
}
public function execute(){
$url = $this->_urlInterface->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
// here pass custom url or you can either use current url on which you are currently and want to come back after logged in.
$loginUrl = $this->_urlInterface->getUrl('customer/account/login', array('referer' => base64_encode($url)));
if($this->_customerSession->isLoggedIn()){
return $this->resultPageFactory->create();
}
$this->_redirect($loginUrl);
}
}`
Passo 2
Vai su Ammin .: Store> Configurazione> Clienti> Configurazione cliente> Opzioni di accesso> Reindirizza dashboard cliente-account dopo aver effettuato l'accesso> No