The Edit Blog

Programatically assigning a Customer Group in Magento

ARTICLE BY Douglas Radburn
    READ TIME: 2 mins
    17th April 2014

    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.


    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("[email protected]");

    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.

    $orders = Mage::getResourceModel('sales/order_collection')
    ->addFieldToFilter('customer_id', $customer->getId())
    ->addAttributeToFilter('state', array('neq' => Mage_Sales_Model_Order::STATE_CANCELED));
    foreach ($order->getAllItems() as $orderItem) {
        if (in_array($orderItem->getProductId(), $products)) {
            $changeGroup = true;
    if ($changeGroup == true) {

    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:

    $customerGroupModel = new Mage_Customer_Model_Group();
    $customerGroups = array();
    $allCustomerGroups  = $customerGroupModel->getCollection()->toOptionHash();
    foreach($allCustomerGroups as $key => $group){
       $customerGroups[$key] = array('value'=>$group,'label'=>$group);

    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.