Sunday, October 17, 2010

PrestaShop Tips - How to display user group name on front store

Sometime, you may want to display Group Name information of current logged customer on front store. At current version of PrestaShop(1.3.1), there seems no way to display group name of customer at front store without customization. Here I explain on how to do some small change so that you can display customer group name at User Info Block.

Note:
A customer could belong to multiple group, this customization only bring the group name with largest id which the customer belongs to.
It is possible to bring all group names, it will need more code. Hope you can do it by yourself.

Here is how to do this
1. Adding getGroupName function to Group class.
File: ./classes/Group.php

Go to the bottom of file Group.php, you find following two lines at bottom

}

?>


Add following code right before above two lines:


static public function getGroupName($id_customer, $id_lang)
{
$sql = '
SELECT gl.`name`
FROM `'._DB_PREFIX_.'group` g
LEFT JOIN `'._DB_PREFIX_.'customer_group` cg ON (cg.`id_group` = g.`id_group` AND cg.`id_customer` = '.intval($id_customer).')
LEFT JOIN `'._DB_PREFIX_.'group_lang` AS gl ON (g.`id_group` = gl.`id_group` AND gl.`id_lang` = '.intval($id_lang) . ')
ORDER BY g.id_group DESC
';
return Db::getInstance()->getValue($sql);
}


2. Getting group name and set to variable
File: ./init.php
Add following line before line 158 and before line 200 (which are exiting lines for customerName)


'groupName' => ($cookie->id_customer ? Group::getGroupName(intval($cookie->id_customer),intval(_USER_ID_LANG_)) : false),


3. Use it in User Info Block
If you have not installed this block, please do so by go Back Office - Modules - User Info Block.

At line 6 of ./modules/blockuserinfo.tpl, add ", group {$groupName}" after {$customerName}

Before change:



After change:

{$customerName}, group {$groupName} ({l s='Log out' mod='blockuserinfo'})


You are all set. You should be able to see group name of your customer displayed along with his/her name at front store now.

2 comments:

Anonymous said...

It throws me this error:

Fatal error: Call to undefined method Group::getGroupName() in C:\xampp\htdocs\prestashop\init.php on line 162

and here are my lines around 162:

'page_name' => $page_name,
'customerName' => ($cookie->logged ? $cookie->customer_firstname.' '.$cookie->customer_lastname : false),
'groupName' => ($cookie->id_customer ? Group::getGroupName(intval($cookie->id_customer),intval(_USER_ID_LANG_)) : false),
'priceDisplay' => $priceDisplay,


Any hope?

Thank you for your valuable time :o)

Angelo
WisdOM Books.com
angelo.pugliese@yahoo.com

Alvin said...

From the error message, I guess you have not done step 1. correctly.

You must add function getGroupName to ./classes/Group.php file as instructed in the posted.

Please note:
You must add the code in STEP 1 to the bottom of Group.php file, right before following two lines


}

?>