Get Multiple Customer Groups In Magento with GroupsCatalog2

26

Feb

Get Multiple Customer Groups In Magento with GroupsCatalog2

Get Multiple Customer Groups In Magento with GroupsCatalog2

Sometimes we need to get a customer’s group in Magento. Pretty straight forward. But, when you add GroupsCatalog2, customers can be assigned to multiple groups. I recently had a client that wanted to tell customers on their customer dashboard what groups they are in. With the extension the customer’s group could be stored as “1”, or as “1,2,33,22,44” etc. I thought I’d share how that can be done.

//Check if the customer is logged in get session.
if(Mage::getSingleton('customer/session')->isLoggedIn()) {

//Get customer from Magento
$customerData = Mage::getSingleton('customer/session')->getCustomer();

//Get customer group ids from custom customer attribute created by CatalogGroups2
$groupids = $customerData->getGroup_id();
$grpArray = explode(',',$groupids);
//print_r($grpArray);

$grpImplode = array();
foreach($grpArray as $key=>$val) {

//Get the group name from Magento
$groupname = Mage::getModel('customer/group')->load($val)->getCustomerGroupCode();

//Add group name to array
$grpImplode[] = $groupname;
}

//print group names
echo implode(', ', $grpImplode);
}

P.S. We showed this on the customer dashboard by putting it in app/design/frontend/(theme package)/(theme)/customer/account/dashboard.phtml. Larry Diffey at DifTech contributed to this.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.