branko ajzele, senior developer / project manager

Posts / Articles

Get the current customer role in Magento

Here’s a little snippet for those of you that need some custom customer coding. In the example below, I’m checking to see if the current user belongs to role named “internal”. You can use similar approach to show or hide some stuff inside your template depending on a customer that is logged in.

<?php
 
$roleId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$role = Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');
$role = strtolower($role);
 
 
if($role == 'internal')
{
    echo 'Hello internal...';
}
 
?>

View Comments

  1. it doesn’t work. I tried to put it in the hello.phtml under template, but it showed below in the frontend :(

    f($role == ‘Wholesale’) { echo ‘Member Type: Wholesale’; } ?>

    could you please help?

  2. it’s is correct and that should be showed wholesale, but the code seems hv some errors …

  3. Code is working… I just tested it, placed the following into the top of header.phtml (but you can place it anywhere)… here you go…


    < ?php

    $roleId = Mage::getSingleton('customer/session')->getCustomerGroupId();
    $role = Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');

    var_dump($role);

    $role = strtolower($role);
    if($role == 'wholesale')
    {
    echo 'Hello wholesale...';
    }

    ?>

    p.s. You Need to be looged in as customer… at http://some_server/customer/account/login/

  4. Hey, Thanks for posting this. Saved me from searching the code to figure it out. Some of your other posts have also been helpfull.

  5. How about getting the admin role, not the customer role?

  6. suraj Surendran /

    how could i get the current customer address

Trackbacks/Pingbacks

  1. TIP - Sacar el rol del usuario | Blog de Magento - [...] activecodeline he encontrado un pequeño truco para poder sacar el rol del usuario que está navegando por [...]
blog comments powered by Disqus