문제

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

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 ) {}
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top