Jump to content
  • 0
Bartjuh1994

Get Customer Group Id In Smarty

Question

Hi all,

 

How can i get the customer group id in smarty (Product list page and product detail page)? Now i have the customer ID but i dont wanna know how to change it to the group id.

 

The customer code i have:

{if $cookie->id_customer == 3}

 

Thanks in advance

Share this post


Link to post
Share on other sites

39 answers to this question

Recommended Posts

  • 0

I've never found a global variable for customer group. It's not so simple because the customer group can be more than one number. Look around the forums for customer group. Unless things have changed, you have to extend some of the classes to pass the data to Smarty.

Share this post


Link to post
Share on other sites
  • 0

Hello,

 

I have coded a simple module to do that. Please find it attached.

It will push the groups the actual customer belongs to, into an array and will pass it to smarty. If the user isn't logged in, it will return "FALSE"

 

So, after the module is installed, you can find the customer groups array within your template as in the example below, which will display the ID and the name of the first group :

{if is_array($customerGroups) }{$customerGroups[0]['id_group']} - {$customerGroups[0]['name']}{/if}

 

I hope it helps.

customergroup.zip

Share this post


Link to post
Share on other sites
  • 0

Hello,

 

This is exactly what I was looking. The module shows the customer group.

But if the customer is part of 2 groups, how can I show the default group or both groups ?

 

For example, if 1 client is part of group "Customer" and "Discount 5%" (which is the default group):

 

a. how can I show only group "Discount 5%" ?

b. how can I show or both groups ?

 

Thanks for your help !

Edited by longip

Share this post


Link to post
Share on other sites
  • 0

Customer groups there, is already an array containing all the groups the customer belongs to.

 

If you update the method hookHeader as follows for example,


       public function hookHeader()
       {
               global $smarty;
               $context = Context::getContext();
               $id_lang = $context->cart->id_lang;
               $customer = $context->customer;
               $id_customer = $customer->id;
               $groups = Db::getInstance()->executeS("SELECT " ._DB_PREFIX_. "customer_group.id_group , " ._DB_PREFIX_. "group_lang.name
FROM " ._DB_PREFIX_. "customer_group
                               LEFT JOIN " ._DB_PREFIX_. "group_lang ON " ._DB_PREFIX_. "group_lang.id_group = " ._DB_PREFIX_. "customer_group.id_group
                               WHERE " ._DB_PREFIX_. "customer_group.id_customer = '$id_customer' AND " ._DB_PREFIX_. "group_lang.id_lang = '$id_lang'");
               if(!isset($groups[0])) $groups = FALSE;
               $smarty->assign('customerGroups', $groups);
$defaultGroupId = Configuration::get('PS_CUSTOMER_GROUP');
$defaultGroup = new Group($defaultGroupId);
$smarty->assign('customerDefaultGroup' , $defaultGroup);
       }

... in the template, it will give you access to the whole object for the default group. Then, in the template you can access it as:

{$customerDefaultGroup->name[1]}

and it will print the default group's name, in language id #1, probably the default language.

You can also pass anything else to Smarty extending the module this way.

Share this post


Link to post
Share on other sites
  • 0

Thanks for your help. I do not know php, I just pasted the new code.

But, the new code always shows group "Customer". Thanks anyway.

Share this post


Link to post
Share on other sites
  • 0

Hello,

 

Without any php knowledge, that's already impressive that you could do it that far.

If my code above always returns "customer", then your default group isn't "Discount 5%", but it is still the default, "customer".

This topic about changing the default customer group is a solved case and it might help: http://www.prestashop.com/forums/topic/162675-solved-how-to-set-customer-group-default/

Share this post


Link to post
Share on other sites
  • 0

Sorry for may late reply. Just got back from a short holiday.

 

The first thing I have check was id "Discount 5%" is default group and it is. The code always returns "Customer".

Share this post


Link to post
Share on other sites
  • 0

Hi prestashopninja,

 

Really appreciate this module and your code. This helps with an issue whereby I want to display selective content to various groups.

 

In my case, I really wanted to identify one group. So I added some PHP to the hookHeader function, after the query.

/* assigns smarty variable for corporate accounts */

foreach ($groups as $group) {

if ($group['id_group'] == 26) {

$smarty->assign('tw_corporate', 1);

}

}

 

Now in the templates, I can simply add code like this for the content.

{if $tw_corporate eq 1}

...

{/if}

 

I thought that might be useful to someone else in the same situation.

 

James

Share this post


Link to post
Share on other sites
  • 0

Hi prestashopninja,

 

1- Could you let me know whether your module is compatible with Prestashop 1.4.7.0 ?

 

2- I would like to do the following :

- On the front office, I would like that the voucher box only displays for clients belonging to this specific client group  (group 2)

Would you have any idea how to achieve it ?

 

Thank you in advance for any help in this matter.

 

Patrick

Share this post


Link to post
Share on other sites
  • 0

My thanks to Prestashopninja the code you supplied set me in the right direction but I also found it would only return "Customer".

 

I found replacing the line $defaultGroupId = Configuration::get('PS_CUSTOMER_GROUP');

 

With $defaultGroupId = Customer::getDefaultGroupId((int)$id_customer);

 

This solved it for me, I hope this helps.

Share this post


Link to post
Share on other sites
  • 0

If you want to have default customer group value (customerDefaultGroup) in your smarty template, it's enough to add this code to hookHeader function of any module you use:

 
global $smarty;
$smarty->assign('customerDefaultGroup' , Customer::getDefaultGroupId(Context::getContext()->customer->id));

Share this post


Link to post
Share on other sites
  • 0

This is not beautifull, but it works for me in PS 1.6.

 

Let's say you have the 3 customer groups pluss one custom group. You would like to test if the user is logged in and is a member of the custom group (id 4) (the user can also be a member of other groups).

 

First install prestashopninja's module (thank you prestashopninja). In the template try this:

{if is_array($customerGroups) }
  {if $customerGroups[0]['id_group'] eq '4' || $customerGroups[1]['id_group'] eq '4' || $customerGroups[2]['id_group'] eq '4'  || $customerGroups[3]['id_group'] eq '4'}
    Content for group 4 goes here
  {else}
    You are not in group 4
  {/if}
{else}
  You are not in any customer groups (or not logged in)
{/if}
Edited by me-and-prestashop

Share this post


Link to post
Share on other sites
  • 0
Yes. I'am playing around on a dev installation for now. Was only showing that the zeros in 
{$customerGroups[0]['id_group']} - {$customerGroups[0]['name']}

apparently is referring to the first group ticked off for each user (smallest group id number ticked off). One can also look at the second, third, forth chosen group to see if it's a match.

 

By the way. Is there anything particular unsafe with this raw module? I am looking for a solution to allow only one customer group to see the supplier page and -lists (and site map item). I would not know how to do this in a safer way. 

Edited by me-and-prestashop

Share this post


Link to post
Share on other sites
  • 0

The XML on the zip file needs some adjustment.  It's a copy of the Ultimate XML Importer.

<?xml version="1.0" encoding="UTF-8" ?>
        <module>
            <name>customergroup</name>
            <displayName><![CDATA[Customer Group]]></displayName>
            <version><![CDATA[1]]></version>
            <description><![CDATA[Assigns Customer Groups to Smarty Variable]]></description>
            <author><![CDATA[PrestashopNinja.com]]></author>
            <tab><![CDATA[migration_tools]]></tab>
            <is_configurable>1</is_configurable>
            <need_instance>1</need_instance>
	<limited_countries></limited_countries>
        </module>

Is there a way to get all the values in the array?

 

How about the Default Group?

 

This module is the only method I have found to assign the Customer Group to a smarty variable.  Old overrides no longer work in 1.6.

 

Thanks for the good work.

Edited by melriks

Share this post


Link to post
Share on other sites
  • 0

Hello is possible to have the module to take groups of clients?
I need to see the id and group names on products.tpl and list_products.tpl.

Thank you and sorry for my english!

Share this post


Link to post
Share on other sites
  • 0

Tks Prestaninja for your work ... together with Jamess contribution on post #9 .... you both saved my day!
perfectly working on PS 1.6.0.14

Share this post


Link to post
Share on other sites
  • 0

Hi, how to get help in the card vendor item number.
This article is just a group of customers with ID4 will have to see.
Access all others should be closed.
I have this code that I used in produkt.tpl:

<p id="supplier_reference" {if !$product->supplier_reference}style="display: none;"{/if}>
<label for="manufacturer_name">{l s='ART :'} </label>
<span class="editable">{$product->supplier_reference|escape:'htmlall':'UTF-8'}</span><br>

Sorry for my English :)

Share this post


Link to post
Share on other sites
  • 0

Hello,

 

I have coded a simple module to do that. Please find it attached.

It will push the groups the actual customer belongs to, into an array and will pass it to smarty. If the user isn't logged in, it will return "FALSE"

 

So, after the module is installed, you can find the customer groups array within your template as in the example below, which will display the ID and the name of the first group :

{if is_array($customerGroups) }{$customerGroups[0]['id_group']} - {$customerGroups[0]['name']}{/if}
I hope it helps.

 

 

Thanks from Italy too. 

You made my day.

Share this post


Link to post
Share on other sites
  • 0

The same solution works perfectly for me on product_list page, but not in the product_list presented in search.tpl. In search results the whole if-else statement is completely ignored. Please, can anyone explain why?

Share this post


Link to post
Share on other sites
  • 0

gosh, so much overcomplicated.... why not:

{Group::getCurrent()->id}

will make the customer (user) default (most specific one) group available in template (tested on PS 1.6.1.1 from product.tpl)

Edited by prestowicz

Share this post


Link to post
Share on other sites
  • 0

Customer groups there, is already an array containing all the groups the customer belongs to.

 

If you update the method hookHeader as follows for example,


        public function hookHeader()
        {
                global $smarty;
                $context = Context::getContext();
                $id_lang = $context->cart->id_lang;
                $customer = $context->customer;
                $id_customer = $customer->id;
                $groups = Db::getInstance()->executeS("SELECT " ._DB_PREFIX_. "customer_group.id_group , " ._DB_PREFIX_. "group_lang.name
FROM " ._DB_PREFIX_. "customer_group
                                LEFT JOIN " ._DB_PREFIX_. "group_lang ON " ._DB_PREFIX_. "group_lang.id_group = " ._DB_PREFIX_. "customer_group.id_group
                                WHERE " ._DB_PREFIX_. "customer_group.id_customer = '$id_customer' AND " ._DB_PREFIX_. "group_lang.id_lang = '$id_lang'");
                if(!isset($groups[0])) $groups = FALSE;
                $smarty->assign('customerGroups', $groups);
$defaultGroupId = Configuration::get('PS_CUSTOMER_GROUP');
$defaultGroup = new Group($defaultGroupId);
$smarty->assign('customerDefaultGroup' , $defaultGroup);
        }
... in the template, it will give you access to the whole object for the default group. Then, in the template you can access it as:

{$customerDefaultGroup->name[1]}
and it will print the default group's name, in language id #1, probably the default language.

You can also pass anything else to Smarty extending the module this way.

 

 

 

Thx for providing your module. I have a question about it. I have installed the module, and when I put the snippet into a template file, I get as expected this:

 

4 - Reseller

 

but now I want to use this to hide content for selected usergroups. For example, the user group is "4" I want to display something, else nothing.

 

I already tested 

 

{if $customerDefaultGroup == 4}

 

something

 

{/if}

 

but that does not work. Can you tell me how I could have that worked? ( I am on 1.6.1.7)

 

Thanks in advance

Share this post


Link to post
Share on other sites
  • 0

Thx for providing your module. I have a question about it. I have installed the module, and when I put the snippet into a template file, I get as expected this:

 

4 - Reseller

 

but now I want to use this to hide content for selected usergroups. For example, the user group is "4" I want to display something, else nothing.

 

I already tested 

 

{if $customerDefaultGroup == 4}

 

something

 

{/if}

 

but that does not work. Can you tell me how I could have that worked? ( I am on 1.6.1.7)

 

Thanks in advance

 

Try :

{if Group::getCurrent()->id == 4}

{* YOUR CODE *}

{/if}

Share this post


Link to post
Share on other sites
  • 0

You are right!

 

I thought the code was working in dependece of prestashopninjas module. But now I have tested and it works just as it is, that simple. Great solution.

 

thx for sharing.

Share this post


Link to post
Share on other sites
  • 0

 

In the PDF invoice TPL file, you'll need to use:

{Customer::getDefaultGroupId($order->id_customer)}

Thanks! this does work.

 

I would like to be more specific if possible. For instance, the customer group 4 is a discount card holder group, so I'd like to display "Discount Card Holder" on the invoice page, for each order placed by someone in group 4. Is this possible?

Share this post


Link to post
Share on other sites
  • 0

gosh, so much overcomplicated.... why not:

{Group::getCurrent()->id}

will make the customer (user) default (most specific one) group available in template (tested on PS 1.6.1.1 from product.tpl)

Perfect !!! Greetings from Romania!!

Share this post


Link to post
Share on other sites
  • 0

There is no module I'm aware of to do this. You must manually edit a Smarty template and add code like {if Group::getCurrent()->id == 1}Do stuff for group 1{/if}

Share this post


Link to post
Share on other sites

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

×