Kelas mana yang harus saya timpa untuk mengarahkan pelanggan ke halaman tertentu setelah login?
Saya telah mencoba mengatur Redirect Customer to Account Dashboard after Logging in
konfigurasi toko tetapi tidak berfungsi.
Kelas mana yang harus saya timpa untuk mengarahkan pelanggan ke halaman tertentu setelah login?
Saya telah mencoba mengatur Redirect Customer to Account Dashboard after Logging in
konfigurasi toko tetapi tidak berfungsi.
Jawaban:
Plugin adalah solusi yang lebih baik dalam hal ini karena kelas tambahan Anda mungkin perlu diperbarui ketika Magento 2 memperbarui.
Berikut adalah solusi menggunakan after-plugin pada LoginPost-> execute () seperti yang disarankan oleh 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;
}
}
Saya mengatasinya dengan mengganti kelas LoginPost
etc / di.xml
<preference for="Magento\Customer\Controller\Account\LoginPost" type="Vendor\Module\Controller\Account\LoginPost" />
Vendor / Modul / Pengendali / Akun / 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()
akan menjadi opsi yang lebih bersih
Bahwa penyimpanan lokal saat ini menyebabkan masalah kami.
Jika kami mengaktifkan atau menonaktifkan Redirect Customer to Account Dashboard after Logging in
dan Guest Checkout dalam Konfigurasi, fitur ini akan berfungsi dengan baik. Namun, kami perlu menghapus penyimpanan lokal Anda.
Kami dapat memeriksa penyimpanan lokal localStorage.getItem('mage-cache-storage')
.
Lihatlah:
vendor / magento / module-checkout / 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 akan menetapkan cookie $.cookie('login_redirect', this.options.url.checkout)
berdasarkan customerData
dari penyimpanan lokal.
Dari pengontrol vendor/magento/module-customer/Controller/Account/LoginPost.php
. Ini akan memeriksa URL pengalihan dari cookie.
$redirectUrl = $this->accountRedirect->getRedirectCookie();
if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectUrl) {
......
return $resultRedirect;
}
Versi Magento:
-Magento versi 2.1.0
Saya menyelesaikan ini dengan melewati referer di pengontrol modul kustom.
Langkah1 `
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);
}
}`
Langkah 2
Buka Admin: Toko> Konfigurasi> Pelanggan> Konfigurasi Pelanggan> Opsi Masuk> Arahkan Pelanggan ke Dasbor Akun setelah Masuk> Tidak