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?

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

1 Answer 1

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 ) {}
    }
}
3
  • Thank you, i will use your second solution because i need assign different groups based on data provided at registration.
    – zsd
    Nov 25, 2013 at 12:58
  • I figured something like that :) glad to have helped Nov 25, 2013 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, 2015 at 9:42

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .