Bartjuh1994 Posted February 25, 2013 Share Posted February 25, 2013 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 Link to comment Share on other sites More sharing options...
melriks Posted April 24, 2013 Share Posted April 24, 2013 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 More sharing options...
prestashopninja Posted April 24, 2013 Share Posted April 24, 2013 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 13 Link to comment Share on other sites More sharing options...
longip Posted May 4, 2013 Share Posted May 4, 2013 (edited) 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 May 4, 2013 by longip (see edit history) Link to comment Share on other sites More sharing options...
prestashopninja Posted May 4, 2013 Share Posted May 4, 2013 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. 2 Link to comment Share on other sites More sharing options...
longip Posted May 5, 2013 Share Posted May 5, 2013 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. Link to comment Share on other sites More sharing options...
prestashopninja Posted May 5, 2013 Share Posted May 5, 2013 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 More sharing options...
longip Posted May 7, 2013 Share Posted May 7, 2013 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". Link to comment Share on other sites More sharing options...
jamess Posted July 10, 2013 Share Posted July 10, 2013 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 1 Link to comment Share on other sites More sharing options...
patrmich Posted November 28, 2013 Share Posted November 28, 2013 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 More sharing options...
MX-Drew Posted June 6, 2014 Share Posted June 6, 2014 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. 1 Link to comment Share on other sites More sharing options...
CUBE-IN Posted November 11, 2014 Share Posted November 11, 2014 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)); 1 Link to comment Share on other sites More sharing options...
me-and-prestashop Posted November 17, 2014 Share Posted November 17, 2014 (edited) 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 November 17, 2014 by me-and-prestashop (see edit history) Link to comment Share on other sites More sharing options...
prestashopninja Posted November 17, 2014 Share Posted November 17, 2014 The module was only there to give an idea. I don't advise to use it in any production environment as it's raw and I hope it can describe the general logic to pass data to Smarty using a custom module. Link to comment Share on other sites More sharing options...
me-and-prestashop Posted November 17, 2014 Share Posted November 17, 2014 (edited) 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 November 18, 2014 by me-and-prestashop (see edit history) Link to comment Share on other sites More sharing options...
prestashopninja Posted November 17, 2014 Share Posted November 17, 2014 From the view of safety, it should be pretty safe, yet it's a just simple example; not meant to use in any production environment. Link to comment Share on other sites More sharing options...
melriks Posted April 5, 2015 Share Posted April 5, 2015 (edited) 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 April 5, 2015 by melriks (see edit history) Link to comment Share on other sites More sharing options...
roberto77 Posted April 7, 2015 Share Posted April 7, 2015 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! Link to comment Share on other sites More sharing options...
Pablo_Lopez Posted April 14, 2015 Share Posted April 14, 2015 Thanks prestashopninja! Link to comment Share on other sites More sharing options...
matracon Posted June 23, 2015 Share Posted June 23, 2015 Thanks you! prestashopninja Link to comment Share on other sites More sharing options...
ariom Posted July 13, 2015 Share Posted July 13, 2015 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 Link to comment Share on other sites More sharing options...
Виталий85 Posted February 24, 2016 Share Posted February 24, 2016 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 More sharing options...
Shellanza Posted May 25, 2016 Share Posted May 25, 2016 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 More sharing options...
sueshi Posted June 13, 2016 Share Posted June 13, 2016 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 More sharing options...
prestowicz Posted August 26, 2016 Share Posted August 26, 2016 (edited) 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 August 26, 2016 by prestowicz (see edit history) 3 Link to comment Share on other sites More sharing options...
dusticelli Posted September 20, 2016 Share Posted September 20, 2016 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 More sharing options...
prestowicz Posted September 21, 2016 Share Posted September 21, 2016 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} 3 1 Link to comment Share on other sites More sharing options...
dusticelli Posted September 21, 2016 Share Posted September 21, 2016 @ prestowicz, thx a lot. That works like a charm. Link to comment Share on other sites More sharing options...
prestowicz Posted September 21, 2016 Share Posted September 21, 2016 @ prestowicz, thx a lot. That works like a charm. the best part, no module needed, inbuild presta functionality 1 Link to comment Share on other sites More sharing options...
dusticelli Posted September 21, 2016 Share Posted September 21, 2016 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. Link to comment Share on other sites More sharing options...
gazzza182 Posted October 20, 2016 Share Posted October 20, 2016 Hi This is brilliant!! Is there any way of getting it to display on the PDF Invoice?? It's good to know when ringing orders through a till point. Link to comment Share on other sites More sharing options...
MReds Posted October 21, 2016 Share Posted October 21, 2016 Thanks from Italy too. You made my day. ...mine too! ...and Italy too! Link to comment Share on other sites More sharing options...
Grafeco Posted October 27, 2016 Share Posted October 27, 2016 Por si alguien lo sigue buscando, es: {$customerDefaultGroup->id} Un saludo. Link to comment Share on other sites More sharing options...
gazzza182 Posted November 2, 2016 Share Posted November 2, 2016 Por si alguien lo sigue buscando, es: {$customerDefaultGroup->id} Un saludo. Will I be able to get this to work on invoice.tpl for the PDF invoice? Link to comment Share on other sites More sharing options...
rocky Posted November 3, 2016 Share Posted November 3, 2016 In the PDF invoice TPL file, you'll need to use: {Customer::getDefaultGroupId($order->id_customer)} Link to comment Share on other sites More sharing options...
gazzza182 Posted November 4, 2016 Share Posted November 4, 2016 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 More sharing options...
Cadmiel Posted March 28, 2017 Share Posted March 28, 2017 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 More sharing options...
[email protected] Posted October 20, 2017 Share Posted October 20, 2017 (edited) Hello, where i find this module ? I wants catch client group id in smarty. Prestashop 1.6.1.17. Edited October 20, 2017 by [email protected] (see edit history) Link to comment Share on other sites More sharing options...
rocky Posted October 21, 2017 Share Posted October 21, 2017 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} Link to comment Share on other sites More sharing options...
templaries Posted November 6, 2017 Share Posted November 6, 2017 You can use this on smarty template: {Customer::getDefaultGroupId(Context::getContext()->customer->id)} 1 1 Link to comment Share on other sites More sharing options...
nikos83 Posted February 27, 2019 Share Posted February 27, 2019 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 More sharing options...
Ilya Butenko Posted October 7, 2019 Share Posted October 7, 2019 {Context::getContext()->customer->getGroups()} - it will return array with customer groups other variants with {$customer} and {Customer::getGroupsStatic($customer.id)} NOT worked for me I used prestashop 1.7.6.1 Link to comment Share on other sites More sharing options...
toneko Posted February 21, 2020 Share Posted February 21, 2020 The hookHeader code works flawless in Presta 1.7.5.1. Thank you @prestashopninja!! Link to comment Share on other sites More sharing options...
sg1_anhell Posted March 28, 2023 Share Posted March 28, 2023 This solution solved my problem in Prestashop 1.7 : https://www.hiddentechies.com/blog/prestashop/get-current-customer-group-id-in-prestashop/ {$customer.id_default_group} Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now