I created a Plugin for sending emails when the owner of the shop changes the customer's group in backend. I still need to add the group (discount) in the email. The problem is that when I access to a customer and change it's group it sends me around 16 emails, the function that sends the email is afterSetGroupId();
so i don't understand why it sends me too many emails.
Here is my code: Plugin SendDiscount.php
public function afterSetGroupId(\Magento\Customer\Api\Data\CustomerInterface $customer, $result)
{
//$groupId = $customer->getGroupId();
//$this->sendYourCustomEmail($customer);
/* 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']];
$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());
}
}
This is my di.xml located under etc/adminhtml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="\Magento\Customer\Api\Data\CustomerInterface">
<plugin name="PHPCuong_CustomerGroupEmail_Plugin" type="PHPCuong\CustomerGroupEmail\Plugin\SendDiscount" sortOrder="10" disabled="false" />
</type>
</config>