Friday 24 July 2015

Resolved Magento notify a customer by an email on its group change by admin.

Magento the widely known eCommerce store gives its users the facility to group its customers. This feature comes in handy when details need to be displayed to specific group related to some discount or offer valid for a small number. However, the problem arises when the customers are not aware of this subtle distinction and miss a chance to avail the brownie points. This happens because Magneto is not having a default feature to signal the customer regarding the group change.
To resolve this issue one can create a module by going through the following steps:

Step 1.
Create Module xml file app\etc\modules\Magcustom_Customergroup.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Magcustom_Customergroup>
            <active>true</active>
            <codePool>community</codePool>
        </Magcustom_Customergroup>
    </modules>
</config>

Step2.
Create a Observer app\code\community\Magecustom\Customergroup\etc\config.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Magcustom_Customergroup>
            <version>0.1.0</version>
        </Magcustom_Customergroup>
         <customergroup>
            <class>Magcustom_Customergroup_Model</class>
        </customergroup>
    </modules>
   
    <global>
        <events>
            <customer_save_after>
                <observers> 
                    <stockalert> 
                        <type>singleton</type>
                            <class>Magcustom_Customergroup_Model_Observer</class>
                            <method>checkDisplay</method>
                    </stockalert>
                </observers>
            </customer_save_after>
        </events>
    </global>
</config>

 Create a Observer app\code\community\Magecustom\Customergroup\Model\Observer.php

<?php
class Magcustom_Customergroup_Model_Observer
{
    public function checkDisplay(Varien_Event_Observer $observer){

        $customer=$observer->getEvent()->getCustomer();
        Mage::log('My log entry'.$customer->getId(), null, 'Magento37890.log');
        /* if customer  is old customer */
       
        if($customer->getId()){
       
            if($customer->getOrigData('group_id')!= $customer->getData('group_id')){            
                $oldgroupname = Mage::getModel('customer/group')->load($customer->getOrigData('group_id'))->getCustomerGroupCode();
                $groupname = Mage::getModel('customer/group')->load($customer->getData('group_id'))->getCustomerGroupCode();
                $name= $customer->getName();
           
            /* here you write your code to send whenever you have change the group */
           

            $templateId = 2;

            $sender  = array(
            'name' => Mage::getStoreConfig('trans_email/ident_support/name', Mage::app()->getStore()->getId()),
            'email' => Mage::getStoreConfig('trans_email/ident_support/email', Mage::app()->getStore()->getId())
            );
           
            $vars  = array(
            'CustomerName' => $customer->getName(),
            'CustomerEmail' => $customer->getEmail(),
            'CustomerGroupname' => $groupname,           
            'CustomerOldGroupname' => $oldgroupname
            );
           
   
            $customerEmail=$customer->getEmail();
            $customerName=$name;
           Mage::getModel('core/email_template')->sendTransactional($templateId, $sender, $customerEmail, $customerName, $vars, $storeId);
                    }
        }else{
        /* New Customer */

        }
    }
}


Step 3.

Create a email template system-> Transactional Emails

Template Name  : Custom Group Change Email
Template Subject  : Group Change, {{var CustomerName}}!

Template Content:

{{template config_path="design/email/header"}}
{{inlinecss file="email-inline.css"}}

<table cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td class="action-content">
            <h1>Dear  {{var CustomerName}},</h1>
            <p> Your group has changed from <strong> {{var CustomerOldGroupname}}</strong> to <strong>{{var CustomerGroupname}}</strong>.</p>
            <p>
                If you have any questions, please feel free to contact us at
                <a href="mailto:{{var store_email}}">{{var store_email}}</a>
                {{depend store_phone}} or by phone at <a href="tel:{{var phone}}">{{var store_phone}}</a>{{/depend}}.
            </p>
        </td>
    </tr>
</table>

{{template config_path="design/email/footer"}}

No comments:

Post a Comment