Question

I want to find a solution for sending an email to the customer when the administrator in backend changes its customer group. I have 3 customer groups, each one has different discounts in the store, so if I switch the user to another customer group, I want to send an email telling about the new discount.

I was thinking in an observer because I have made some observers to send an email with different events. But what would it be the event for this situation? something like adminhtml_customer_update.

I also guess I would need to get the group where the customer is assigned. Thank you! Plugin code:

public function afterSetGroupId(\Magento\Customer\Api\Data\CustomerInterface $customer, $result)
    {
        //$groupId = $customer->getGroupId();
        //$this->sendYourCustomEmail($customer);

        $customer_group = $customer->getGroupId();
        /* Receiver Detail the person who is going to receive the info that user logged in*/
        $receiverInfo = [
            'name' => 'Admin',
            'email' => 'ovazquez@transom-group.com'
        ];

        $store = $this->storeManager->getStore();

       $templateParams = ['store' => $store, 'administrator_name' => $receiverInfo['name'], 'customer_group' => $customer_group];

        $transport = $this->transportBuilder->setTemplateIdentifier(
            'phpcuong_transactional_email_customer_group'
        )->setTemplateOptions(
            ['area' => 'frontend', 'store' => $store->getId()]
        )->addTo(
            $receiverInfo['email'], $receiverInfo['name']
        )->setTemplateVars(
            $templateParams
        )->setFrom(
            'general'
        )->getTransport();

        try {
            // Send an email
            $transport->sendMessage();
        } catch (\Exception $e) {
            // Write a log message whenever get errors
            $this->logger->critical($e->getMessage());
        }       
    }
Was it helpful?

Solution

You could also go for the observer route, you can check all the events that are triggered here (for Magento 2.3). In this case I would try the event adminhtml_customer_save_after event (triggered here \Magento\Customer\Controller\Adminhtml\Index\Save::execute). The event contains data about the request and the customer, so it will be enough to work with.

Instead of an observer, you could use also use a plugin. Magento does the same thing in the module-customer. Check the file vendor/magento/module-newsletter/etc/di.xml. You can find an example of a customer plugin here:

<?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\Api\CustomerRepositoryInterface">
        <plugin name="update_newsletter_subscription_on_customer_update"
                type="Magento\Newsletter\Model\Plugin\CustomerPlugin"/>
    </type>
</config>

You can do the same with your custom module. I would try to create an 'after' plugin for \Magento\Customer\Api\Data\CustomerInterface::setGroupId first. You can then send the email from there. Something like this:

class CustomerInterfacePlugin
{
    public function afterSetGroupId(\Magento\Customer\Api\Data\CustomerInterface $customer, $result)
    {
        $groupId = $customer->getGroupId();
        $this->sendYourCustomEmail($customer);
    }
}

One sidenote: if you want the plugin to only trigger in the Magento 2 backend, use the adminhtml area. More info here

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top