Plugin Before Onepage Dispatch
-
07-01-2021 - |
Domanda
I have the following plugin:
<?php
namespace Company\Module\Plugin;
class DisableLoginCheckout
{
/**
* @var \Magento\Customer\Model\Session
*/
protected $customerSession;
/**
* @var \Magento\Customer\Model\Group
*/
protected $customerGroupCollection;
/**
* @var \Magento\Framework\Controller\Result\RedirectFactory
*/
protected $redirectFactory;
/**
* @var \Magento\Framework\App\Response\RedirectInterface
*/
protected $redirectInterface;
/**
* @var \Magento\Framework\Message\ManagerInterface
*/
protected $messageManager;
/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
public function __construct(
\Magento\Customer\Model\Session $customerSession,
\Magento\Customer\Model\Group $customerGroupCollection,
\Magento\Framework\Controller\Result\RedirectFactory $redirectFactory,
\Magento\Framework\App\Response\RedirectInterface $redirectInterface,
\Magento\Framework\Message\ManagerInterface $messageManager,
\Psr\Log\LoggerInterface $logger
) {
$this->_customerSession = $customerSession;
$this->_customerGroupCollection = $customerGroupCollection;
$this->resultRedirectFactory = $redirectFactory;
$this->redirect = $redirectInterface;
$this->messageManager = $messageManager;
$this->_logger = $logger;
}
public function beforeDispatch(
\Magento\Checkout\Controller\Onepage $subject,
\Magento\Framework\App\RequestInterface $request
){
if ($this->getCustomerGroup() == 'Disabilitato') {
$lastCustomerId = $this->_customerSession->getCustomerId();
$this->_customerSession->logout()->setBeforeAuthUrl($this->redirect->getRefererUrl())
->setLastCustomerId($lastCustomerId);
//$this->messageManager->addNoticeMessage(__('Your account has not been enabled yet'));
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('accuontdisabled', ['_secure' => true]);
$result = $resultRedirect;
return $result;
}
}
public function getCustomerGroup()
{
$currentGroupId = $this->_customerSession->getCustomer()->getGroupId();
$collection = $this->_customerGroupCollection->load($currentGroupId);
return $collection->getCustomerGroupCode();
}
}
when I try to login with authentication-popup i've got this error
Fatal error: Uncaught TypeError: Argument 2 passed to Magento\Store\App\Action\Plugin\StoreCheck::beforeDispatch() must implement interface Magento\Framework\App\RequestInterface, instance of Magento\Store\App\Response\Redirect given
Any help?
EDIT
Company/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\Checkout\Controller\Onepage">
<plugin name="MyPlugin" type="Company\Module\Plugin\DisableLoginCheckout" />
</type>
</config>
EDIT 2
before plugin cannot be used in this way, after plugin solves.
Soluzione
Your plugin implementation is fine. However, the return value is a controller of type redirect.
Subsequently, when the dispatch function kicks in after your plugin, it receives a redirect object and that's why you have this error.
Instead, I would try an around plugin myself so that you can keep a similar implementation but with the idea not to break the dispatch function anymore.
Altri suggerimenti
before plugin cannot be used in this way, after plugin solves.