How to assign a group to user programmatically in magento

3 comments
Sometimes, we need to create an observer to assign a group to user programatically. Because there is no rule available in Magento to change user group as we set the rules for shopping cart and catalog. Although, an option is available in Magento System->Configuration->Customers->Customer Configuration-> Create New Account Option. But if you set a group then all new users will be assigned to it. And in some cases, we need to assign it programmatically.

So here is the simple three lines of code:

$customer = $observer->getEvent()->getCustomer();
$customer->setData('group_id', 3); // or whatever the group id you want to assign
$customer->save();

Hope this small tutorial will save your time. Thanks for reading my post.

3 comments :

  1. How much money you earned from this blog from starting date till today? Using adsense on your blog?

    ReplyDelete
  2. Thanks for this Great sharing i like this post and your blog is amazing. i am a big fan of this blog..:) magento product designer

    ReplyDelete