How to assign a group to user programmatically on registration
-
16-10-2019 - |
문제
I would like to assign role to users on registration from my module.
I checked the createPostAction()
method of Mage_Customer_AccountController
class where i found these lines:
$customer->save();
Mage::dispatchEvent('customer_register_success',
array('account_controller' => $this, 'customer' => $customer)
);
Can i use event/observer ['customer_register_success'] to assign the group? Should i create an event observer class in my module or there are better approaches?
해결책
You can do this from the backend.
Go to System > Configuration > Customers > customer configuration > Create new account options
and choose the group you want to assign to in the Default Group
option
This way you don't need to change any code. If you do however want to use code for some specific reason the customer_register_success
event is the way to go.
Your observer would look something like this
class [Namespace]_[Module]_Model_Observer
{
public function set_group($observer)
{
try {
$customer = $observer->getEvent()->getCustomer();
$customer->setData('group_id', 2); // or whatever the group id should be
$customer->save();
} catch ( Exception $e ) {}
}
}