2

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.

2 Answers 2

0

You can create a plugin to call an action after the Unlock action is executed and be able to use the same Unlock class that you already have.

Create file di.xml under

app/code/MyVendor/MyModule/etc/adminhtml/

<?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\Adminhtml\Locks\Unlock">
        <plugin name="send_mail_customer_unlock" type="MyVendor\MyModule\Plugin\Controller\Adminhtml\Locks\Unlock" sortOrder="1"/>
    </type>
</config>

In your Unlock class you must call afterExecute() to send email after unlock the customer.

public function afterExecute(
    \Magento\Customer\Controller\Adminhtml\Locks\Unlock $subject,
    $result
)
{
    $customerId = $subject->getRequest()->getParam('customer_id');
    try {
        $customer = $this->_customerRepositoryInterface->getById($customerId);
        $this->sendEmail($customer);
    } catch (\Exception $e) {
        $this->messageManager->addError($e->getMessage());
    }
    return $result;
}

public function sendEmail($customer)
{
    // Send email.
}

You have to clean cache and re-run compile command in case you are working with compilation

1
  • Thanks, this function really helped me achieve what I needed. Jun 8, 2018 at 16:28
0

Try following way:

app/code/VendorName/ModuleName/etc/adminhtml/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\Model\Authentication">
        <plugin name="VendorName_ModuleName::Authentication" type="VendorName\ModuleName\Plugin\Customer\Model\Authentication" sortOrder="1"/>
    </type>
</config>

app/code/VendorName/ModuleName/Plugin/Customer/Model/Authentication.php


namespace VendorName\ModuleName\Plugin\Customer\Model;


class Authentication
{
    public function aroundUnlock(
        \Magento\Customer\Model\Authentication $subject,
        \Closure $proceed,
        $customerId
    ) {
        try {
            $proceed($customerId);

            $this->sendEmail();
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    private function sendEmail()
    {
        error_log('hey');
    }
}
6
  • Hi, thanks for your prompt response. I did it like you showed me, and in the sendEmail function y tried to exit the app and print a "Hello World" but nothing happens, when clicking the unlock link/button it stills unlocks the customer and the app doesn't stop. Jun 3, 2018 at 15:27
  • Don't use exit, you can do it error_log or magento log system. Also here you have customer_id, you can easily load customer and got all customer information.
    – Sohel Rana
    Jun 3, 2018 at 15:33
  • Hi, it's me again, I tried doing like you told me to, and also I tried to implement a logger, still I can't see the line in the log, I checked various log files (apache, php, magento) and I still can't see it. just for clarification, the module is already installed, I created the proper registration.php file and the etc/module.xml file as well. Jun 3, 2018 at 17:03
  • This is tested code. Could you check again and clear cache?
    – Sohel Rana
    Jun 3, 2018 at 18:31
  • I've checked it by clearing cache, running setup:upgrade and also setup:di:compile, and so far I haven't been able to find the "hey" in the error log, am I doing something else wrong? I have my Vendor, Module folders well created, also I've been really careful with the namespace and other places where the vendor_moduleName convention is used. I'm really grateful for your help. Jun 3, 2018 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.