5

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?

  • What do you mean by "a role"? – Marius Nov 25 '13 at 11:15
  • Sorry i mean Group ;) – zsd Nov 25 '13 at 11:18
14

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

Set up default group

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 ) {}
    }
}
  • Thank you, i will use your second solution because i need assign different groups based on data provided at registration. – zsd Nov 25 '13 at 12:58
  • I figured something like that :) glad to have helped – Sander Mangel Nov 25 '13 at 13:03
  • If you have to change the group after some other events you should get the $customer like this: $customer = Mage::getSingleton('customer/session')->getCustomer(); – Stelian Dec 4 '15 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.