Coding Notes Magento

Send Welcome Email On Customer Group Change In Magento 1.9

For this particular problem I needed a solution that sends a “welcome” email whenever the user gets changed to a specific customer group. The reason being, the store owner needed to manually verify the customer’s registration information before accepting their registration and allowing them to access certain discounts set by the customer group.

The Solution:

The solution is to add a custom function to the adminhtml_customer_save_after hook and have Magento send an email using a transactional email template.

1. Create a transactional email template with the information you want to send to the user. If you want to use variables in this template you will need to edit the code in step 3 to include it. Right now the only variable set is customer_name.

2. Edit app\code\core\Mage\Adminhtml\etc\config.xml and paste the code below under the events tag.

<adminhtml_customer_save_after>
    <observers>
        <bind_locale>
            <class>adminhtml/observer</class>
            <method>send_new_group_welcome_message</method>
        </bind_locale>
    </observers>
</adminhtml_customer_save_after>

3. Edit app\code\core\Mage\Adminhtml\Model\Observer.php and paste the send_new_group_welcome_message function in the Observer class. Make sure you edit the variables $customer_group_name and $transaction_email_code.

  • $transaction_email_code is  the template name you created in step 1.
  • $customer_group_name is the name of the new customer group you wish to use for the welcome emails.
class Mage_Adminhtml_Model_Observer
{
    public function send_new_group_welcome_message($observer) {
        $customer = $observer->getCustomer();
        $transaction_email_code = 'New Group Welcome Email';
        $customer_group_name = 'new group';

        if ($customer->getId()) {
            if ($customer->getOrigData('group_id') != $customer->getData('group_id')) {

                // check if new custom group is in group name, if so send welcome email.
                $groupname = Mage::getModel('customer/group')->load($customer->getData('group_id'))->getCustomerGroupCode();
                if (strpos(strtolower($groupname), $customer_group_name) !== false) {
                    $transactional_email = Mage::getModel('core/email_template')->loadByCode($transaction_email_code);
                    if ($transactional_email->getTemplateId()) {
                        $processedTemplate = $transactional_email->getProcessedTemplate(array(
                            'customer_name' => $customer->getName()
                        ));
                        $mail = Mage::getModel('core/email')
                            ->setToName($customer->getName())
                            ->setToEmail($customer->getEmail())
                            ->setFromEmail(Mage::getStoreConfig('trans_email/ident_sales/email'))
                            ->setFromName(Mage::getStoreConfig('trans_email/ident_sales/name'))
                            ->setBody($processedTemplate)
                            ->setSubject($transactional_email->getTemplateSubject())
                            ->setType('html');
                            
                        try {
                            $mail->send();
                        } catch (Exception $error) {
                            Mage::log($error->getMessage(), null, 'send_welcome_message.log');
                        }
                    }
                }
            }
        }
    }
}

If everything was added correctly, emails should be sent every time you change the user’s customer group to the one you specified.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.