1

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' => '[email protected]'
        ];

        $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());
        }       
    }
1
  • You can create after plugin for Magento\Customer\Controller\Adminhtml\Group:save method and paste your code for sending an email.
    – anonymous
    Aug 7, 2019 at 5:40

1 Answer 1

2

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

2
  • Thank you! I will give it a try Aug 7, 2019 at 13:44
  • Hello friend, I did the plugin but for some reasons it sends me a lot of emails (16 emails) when I change a customer group in backend, I think it's sending me 2 emails per each customer I have (I have 9) because I can see the email with different customer groups. Also it sends the email just by clicking edit, not after saving the customer, do you know what the problem could it be? The plugin code is in the question, Thanks! Aug 14, 2019 at 21:42

Your Answer

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

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