Jump to content

Check if current user is in a certain group


Recommended Posts

Hello everyone, 


I hope some of you can help on a topic that makes me struggle from a couple of days. I have already found 1-2 similar topics, but nothing worked for me. So let me explain what I am trying to do:


I have a customer group where I want to add these users which I consider loyal, so they use discounts on some products. That works great with "price rules".


But what I want is to print in the header info (near  username) the following:


1. If the current user is in the group "Loyal" - print message "You are a loyal customer and shop with discount" . 


So for this aim I need to check if the current is in this exact group.


Any help on how to accomplish that would be appreciated!

Thnaks in advance!

Link to comment
Share on other sites

Hi Viluletka,


Short introduction: as you might know, a customer can be member of more than one group. There is a 'default group' that it belongs to, but it can also be member of other ones.



To get the default group (-id) of a customer, you can use the function:

public static function getDefaultGroupId($id_customer)
As y can see, it's a 'static' function, that means you don't need to create a customer-object first to use this function.
You normally use it as follows:
$customergroup = Customer::getDefaultGroupId((int)$id_customer);
Curstomer:: relates tot the CLASS Customer, so you use the Class to call the function, (not a created object.)
If you want to get all groups a customer is member of, you can use the function:
public static function getGroupsStatic($id_customer)
(as you see, again a static function, so you can use it like this:
$mycustomergroups = Customer::getGroupsStatic((int)$id_customer);
or, if you already have the $customer object, you can use this function (Which uses the static one, actually)
public function getGroups()

Which you use like this:

$mycustomergroups = $customer->getGroups();




all functions can be found in file: /classes/Customer.php


Hope this helps,


  • Like 3
Link to comment
Share on other sites

Hi pascal,


Thanks for the fully explained answer!


As a newbie in prestashop development,  I am wondering about where should I create the function (I assume in the override/FrontController.php..?) and should I pass it to smarty?


Thanks in advance, you have been very helpful!


Link to comment
Share on other sites

Hi Viluletka,



Make a group "Loyal". Add any discount you want to give to the group



save the group and check the ID number of the new group:



Remember this ID number, as we need to use is soon.



Then prepare a test customer (login as a new customer if you don't have one):

Edit the customer and put the customer in de 'Loyal' group AND set default group to "Loyal"



Now the programming part: (Sample code from PretaShop


Edit the file: /modules/blockuserinfo/blockuserinfo.php (Make backup first, just in case)


Find and add the red line:


'cart' => $this->context->cart,
'cart_qties' => $this->context->cart->nbProducts(),
'logged' => $this->context->customer->isLogged(),
'customerName' => ($this->context->customer->logged ? $this->context->customer->firstname.' '.$this->context->customer->lastname : false),
'loyalcustomer' =>($this->context->customer->logged AND (Customer::getDefaultGroupId((int)$this->context->customer->id) == 4) ? true : false),
'firstName' => ($this->context->customer->logged ? $this->context->customer->firstname : false),
'lastName' => ($this->context->customer->logged ? $this->context->customer->lastname : false),
'order_process' => Configuration::get('PS_ORDER_PROCESS_TYPE') ? 'order-opc' : 'order'
Do you see the blue 4? Change this 4 to the ID number you still remember from above (i.e. the ID number of the "Loyal" group).

Save the file.



Then edit the file: /themes/<your theme folder>/modules/blockuserinfo/blockuserinfo.tpl.

(N.B. If this file doesn't exist, COPY it to this place (using this exact path!) from:

 /modules/blockuserinfo/blockuserinfo.tpl )


So, edit the file (Make a backup, as always)

and find the code below and add red code: (Easy to find using Ctrl-F and search for 'Welcome' )


<p id="header_user_info">
{l s='Welcome' mod='blockuserinfo'}
{if $logged}
{if $loyalcustomer}<span id="loyal_customer">{l s='- You are a loyal customer and shop with discount - ' mod='blockuserinfo'}</span>{/if}
<a href="{$link->getPageLink('my-account', true)|escape:'html'}" title="{l s='View my customer account' mod='blockuserinfo'}" class="account" rel="nofollow"><span>{$cookie->customer_firstname} {$cookie->customer_lastname}</span></a>
<a href="{$link->getPageLink('index', true, NULL, "mylogout")|escape:'html'}" title="{l s='Log me out' mod='blockuserinfo'}" class="logout" rel="nofollow">{l s='Log out' mod='blockuserinfo'}</a>
<a href="{$link->getPageLink('my-account', true)|escape:'html'}" title="{l s='Login to your customer account' mod='blockuserinfo'}" class="login" rel="nofollow">{l s='Login' mod='blockuserinfo'}</a>
<!-- /Block user information module HEADER -->


save the file.


What we did here is:

- if the customer is logged in, it checks if it is a "loyalcustomer". If so, it prints the sentence you wanted in the top header, next to the customer name. If not, it doesn't print this.

- The <span> around the text gives the possibility to add some css code to it (to make it bold, give it a background colour, change the font, etc. etc.).

- The {l s='<our text>'} gives the possibility to change the text in the Back office (Then go to Localization->translation, Installed Modules Translation, BlockUserinfo translations to do this)


All done?

Now reload your front page  (Ctrl-F5), login as the loyal customer your prepared and see if it works.



If you don't see anything yet, maybe try this:

You may need to (TEMPORARILY!!): 

- turn OFF your smarty cache and

- 'Template cache' set to "Recompile templates if the files have been updated"

in Advanced Parameters->Performance

to see the changes. (Don't forget to turn cache back ON afterwards!)

Then reload the page again (Ctrl-F5)


Hope this helps,



  • Like 2
Link to comment
Share on other sites

Great support!


Thank you so much, that did the job excellent!


Just one small add to the if conditions (in case somebody needs it).


{if $logged &&  $loyalcustomer} - print message "Loyal"


{if $logged &&  !$loyalcustomer} - print "Still not loyal"


Hope that this topic would be as useful to someone as to me.


Many thanks to pascal

Link to comment
Share on other sites

  • 7 months later...

Excellent piece of useful code that you can use a little bit of everywhere. In my case I put it in

ProductController.php here:

'privilegedcustomer' =>($this->context->customer->logged AND (Customer::getDefaultGroupId((int)$this->context->customer->id) == 7) ? true : false),
'outOfStockAllowed' => (int)Configuration::get('PS_ORDER_OUT_OF_STOCK'),


So I can use it anywhere in the product.tpl, displaying different versions of the page depending on the default group of the user.


Thanks Pascal!

Link to comment
Share on other sites

  • 6 months later...
  • 6 months later...
  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...