Jump to content

Get Customer Group Id In Smarty


Bartjuh1994

Recommended Posts

  • 1 month later...

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.

Link to comment
Share on other sites

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

  • Like 13
Link to comment
Share on other sites

  • 2 weeks later...

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 (see edit history)
Link to comment
Share on other sites

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.

  • Like 2
Link to comment
Share on other sites

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/

Link to comment
Share on other sites

  • 2 months later...

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

  • Like 1
Link to comment
Share on other sites

  • 4 months later...

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

Link to comment
Share on other sites

  • 6 months later...

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.

  • Like 1
Link to comment
Share on other sites

  • 5 months later...

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));
  • Like 1
Link to comment
Share on other sites

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 (see edit history)
Link to comment
Share on other sites

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 (see edit history)
Link to comment
Share on other sites

  • 4 months later...

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 (see edit history)
Link to comment
Share on other sites

  • 2 months later...
  • 3 weeks later...
  • 7 months later...

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 :)

Link to comment
Share on other sites

  • 3 months later...

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.

Link to comment
Share on other sites

  • 3 weeks later...

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?

Link to comment
Share on other sites

  • 2 months later...
  • 4 weeks later...

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

Link to comment
Share on other sites

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}
  • Like 3
  • Thanks 1
Link to comment
Share on other sites

  • 5 weeks later...

 

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?

Link to comment
Share on other sites

  • 4 months later...

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!!

Link to comment
Share on other sites

  • 6 months later...
  • 3 weeks later...
  • 1 year later...

Hi!

I'm trying to get customer reduction value from group he belongs to. At the moment clients belongs to default group (visitors) also some of the clients are attached to the second group 10% reduction.

I would like to show them reduction value when they're logged in. 

{Group::getCurrent()-> id}
{Customer::getDefaultGroupId(Context::getContext()->customer->id)}

this code give me only id my default group = 1 (for this group reduction is 0) but customer also is in group id = 3 with reduction = 10%.

How to get this information? Do I need get group array somehow ? Any help would be great thanks

Link to comment
Share on other sites

  • 7 months later...
  • 4 months later...
  • 3 years 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...