Get the current customer role in Magento

Get the current customer role in Magento

Looking at the old articles on our website that long for a rewrite, I sometimes stumble upon a gem that can be useful. One of those is the piece of code enables us to do custom stuff if our customer is assigned to a certain customer group.

You can find customer groups in Magento Administration.

  • Customers->Customer Groups is where customer groups are managed.
  • Customers->Manage Customers->Click on a customer->Account Information tab->Customer group dropdown is where you would assign a customer to a group.

The way I see it, the possible implementation of this code would be using it in an observer to add custom javascript to our page’s head if a customer is assigned to the ‘Wholesale‘ group.

Let’s do that.

Begin by registering your module. I presume you’ll know how to do this, or have your existing module you can use.

Now, create a configuration file for your module. Since my example module is called ‘Customergroup‘ with namespace ‘Inchoo‘, the code below goes in

app/code/community/Inchoo/Customergroup/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Inchoo_Customergroup>
            <version>1.0.0</version>
        </Inchoo_Customergroup>
    </modules>
    <global>
        <events>
            <controller_action_layout_generate_blocks_after>
                <observers>
                    <inchoo_customergroup>
                        <type>singleton</type>
                        <class>Inchoo_Customergroup_Model_Observer</class>
                        <method>placeCustomJs</method>
                    </inchoo_customergroup>
                </observers>
            </controller_action_layout_generate_blocks_after>
        </events>
    </global>
</config>

Now, we’ll create our observer.
app/code/community/Inchoo/Customergroup/Model/Observer.php

<?php
class Inchoo_Customergroup_Model_Observer
{
    public function placeCustomJs()
    {
 
        $roleId = Mage::getSingleton('customer/session')->getCustomerGroupId();
        $role = Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');
        $role = strtolower($role);
 
        $headBlock = Mage::app()->getLayout()->getBlock('head');
        if($headBlock && $role=='wholesale')
        {
            $headBlock->addJs('test/myScript.js');
        }
        return $this;
    }
}

This isn’t the only use case. You can, of course, use this anywhere else you see fit. For example showing a message for certain customers, a different newsletter box, etc.

Just keep in mind that $role will equal ‘not logged in‘ if a customer isn’t logged in.

Note: This is a revamp of an article originally written in March 2009.

You made it all the way down here so you must have enjoyed this post! You may also like:

Magento Switchable Install Script Setup Class Damir Korpar
Damir Korpar, | 5

Magento Switchable Install Script Setup Class

Remind customers to place their first order in Magento Marko Martinovic
Marko Martinovic, | 17

Remind customers to place their first order in Magento

Login as Customer – Magento extension Branko Ajzele
Branko Ajzele, | 9

Login as Customer – Magento extension

4 comments

  1. hi..i am new to magento i am creating one e commerce website in it,but i have one problem that different customer group should be able to see different price of same product after login.

  2. Is there a way of showing the customer group type (this might be called customer code) on the new order email?

    I have tried using:

    {{var order.getCustomerGroupCode()}}

    Thanks

  3. Hi, Roshni.
    In this example, the javascript file would need to be placed in: /js/test/myScript.js

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.