This tutorial will help you to answer the question “How we can Automatically create and assign customer group in magento ?”.

For creating and assigning customer group, we have to create :

1/ Create app/code/local/Inbusiness/Group/etc/config.xml

<?xml version="1.0"?>
<config>
<modules>
<Inbusiness_Group>
<version>1.0.0</version>
</Inbusiness_Group>
</modules>
<global>
<models>
<Inbusiness_Group>
<class>Inbusiness_Checkout_Model</class>
</Inbusiness_Group>
</models>
<events>
<customer_save_after>
<observers>
<checkout_automatically_create_group_customer>
<type>singleton</type>
<class>Inbusiness_Group_Model_Observer</class>
<method>createGroup</method>
</checkout_automatically_create_group_customer>
</observers>
</customer_save_after>
<adminhtml_customer_save_after>
<observers>
<admin_automatically_create_group>
<type>singleton</type>
<class>Inbusiness_Group_Model_Observer</class>
<method>createGroup</method>
</admin_automatically_create_group>
</observers>
</adminhtml_customer_save_after>
</events>
</global>
</config>

 

 

2/Create app/code/local/Inbusiness/Group/Model/Observer.php to create specific customer group (customer email) and assign it to the customer.

<?php
class Inbusiness_Group_Model_Observer
{
protected static $taxClass = 3;
public function createGroup(Varien_Event_Observer $observer){

try {
$customer = $observer->getEvent()->getCustomer();

$customerEmail = $customer->getEmail();

$customerGroupId = $customer->getGroupId();
// get name group for checking exists group
$customerGroup = Mage::getModel('customer/group');
$customerGroupData = $customerGroup->getCollection()->getData();
$flagCheck = false;
foreach($customerGroupData as $groupData){
if(in_array($customerEmail,$groupData)) $flagCheck = true;
}
//If not found customer Group then create it
if($flagCheck==false){
$customerGroup->setCode($customerEmail);
$customerGroup->setTaxClassId(self::$taxClass);
$customerGroup->save();
}
$customerGroupData = $customerGroup->getCollection()->getData();
foreach($customerGroupData as $groupData){
if(in_array($customerEmail,$groupData)) $newCustomerGroupId = $groupData['customer_group_id'];
}
if($newCustomerGroupId){
$currentCustomerGroupCode =  $customerGroup->load($customerGroupId)->getCode();
if($currentCustomerGroupCode!==$customerEmail){
$customer->setGroupId($newCustomerGroupId);
$customer->save();
}
}

} catch ( Exception $e ) {

}
}

}

 

3/ Create app/etc/modules/Inbusiness_Group.xml to declare Inbusiness_Group module

 

<?xml version="1.0"?>
<config>
<modules>
<Inbusiness_Group>
<active>true</active>
<codePool>local</codePool>
</Inbusiness_Group>
</modules>
</config>

 

 

Remember to refresh your caches to see the changes. Happy coding !