Programatically assigning a Customer Group in Magento

Magento’s Customer Groups give you the ability to offer different pricing and promotions to customers based upon their grouping. This grouping might be initially set when you add the customer, but it might change over time. You might have one customer in a group, or thousands. The Customer Group might change because you want the specific customer to be in a different group, or you might want to programatically set the Customer Group based on decisions or actions taken by your customer.

Creating Customer Groups

Each customer is initially assigned to one of the following default Customer Groups:

- General
- Not Logged In
- Retailer

However, you can create additional Customer Groups as needed.

magento-admin

Assigning customers to Customer Groups

When customers first create an account, they will be assigned to the Customer Group that is designated in System > Configuration. You can assign customers to new groups by editing them. Navigate to Customer > Manage Customers. To re-assign the customer to a new Customer Group, locate the customer and click the Edit button. Click the Account Information tab in the left column.

You can select a new Customer Group in the Customer Group drop-down. To re-assign the Customer Group for several customers at once, you can use Batch Updates.

Assigning Customers to Customer Groups Programatically

First, we need a customer object to update. Let’s load a customer by their email address.

$_customer = Mage::getModel('customer/customer')->loadByEmail("test@testing.co.uk");

We’re going to use this $_customer object to look up the customer’s order history and then we’ll assign them a new Customer Group based on whether they’ve ordered a specific product in the past.

In the example here, $products would be an array of product Ids to match against (these would be our special products). This could be a static array, or it could be built from a query of category products.

The key code from the above example for setting the Customer Group is our call to setGroupId on the customer object. We pass $groupId – which would be the ID of a Customer Group that you’re wanting to swich users over to.

You can grab the Customer Group ID from tha Magento admin system (Customers | Customer Groups – listed on the left side), or, if you need to, you can grab groups programatically themselves using something like this:

This code will leave you with an array $customerGroups of Customer Groups to use.

So, you should now be able to set a customer’s group through code. This code can be added into an observer, a custom module, or run in a cron task.

As you can see Magento has all the required magic methods that allow you to change the Customer Groups via code. This process means that some really interesting CRM rules can be created to promote loyalty and engage with audiences.

By Douglas Radburn. at 11:45AM on Thursday, 17 Apr 2014

Doug is our Senior Open Source Web Developer since bringing his knowledge and skills to Branded3 in 2009. A founding developer of our Twitition and Competwition platforms, Doug has also been lead in our Open Source Projects on Magento ecommerce solutions and Wordpress CMS platforms. Follow Douglas Radburn on Twitter.

comments

  • http://innoexts.com InnoExts

    Hello Douglas,

    Useful post for the users who want to operate successfully with customer groups in Magento. By the way, we implemented an extension, Customer Groups Manager, that extends standard customer groups settings, that could be also helpful for Magento community.