I'm a newbie in Magento 2, and this past days I've been cracking my head by trying to send an email when clicking the "Unlock" button/link to Unlock a customer on the admin panel. I've done it with a Plugin and an Observer, but so far when clicking the "Unlock" button/link nothing happens, I mean, the normal flow of the application continues and it unlocks the customer. This is the code that I have for the plugin (in the upper part i've already set up the namespace and everything else), I just pasted here the class.
class Unlock
{
/**
* @var \Magento\Customer\Api\CustomerRepositoryInterface
*/
protected $_customerRepositoryInterface;
/**
* @var \Magento\Framework\Event\Manager
*/
protected $_eventManager;
/**
* @var \Magento\Framework\Message\Manager
*/
protected $_messageManager;
/**
* Unlock constructor.
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
* @param \Magento\Framework\Event\Manager $eventManager
* @param \Magento\Framework\Message\Manager $messageManager
*/
public function __construct(
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
\Magento\Framework\Event\Manager $eventManager,
\Magento\Framework\Message\Manager $messageManager
)
{
$this->_customerRepositoryInterface = $customerRepositoryInterface;
$this->_eventManager = $eventManager;
$this->_messageManager = $messageManager;
}
/**
* @param \Magento\Customer\Controller\Adminhtml\Locks\Unlock $subject
* @param $result
* @return mixed
*/
public function beforeExecute(
\Magento\Customer\Controller\Adminhtml\Locks\Unlock $subject,
$result
)
{
$customerId = $subject->getRequest()->getParam('customer_id');
try {
// unlock customer
$customer = $this->_customerRepositoryInterface->getById($customerId);
echo $customer->getEmail();
exit;
} catch (\Exception $e) {
$this->messageManager->addError($e->getMessage());
}
return $result;
}
}
What I'm trying to do here is at least to print the customer email and exit the app, but so far it doesn't reach up to this point. Can anyone point me in the right direction as to at least reach to this point? or is it that I'm not going the right direction with this? Any help will be much appreciated. Thanks.