Jump to content

[SOLVED]How to show customer group name automatically at Front Office?


Recommended Posts

I have sorted customer into different groups manually. But I want to know how to show customer group name at Front Office (account page .etc) automatically.

For example, add group name which a specific customer belongs to after logged in:

Welcome, {$customerName} Log out. You are {GroupName}.

Here {GroupName} is what I am looking for. Please help me on this. How to get this cmd?

Millions thanks in advance.

Alex

Link to comment
Share on other sites

I have sorted customer into different groups manually. But I want to know how to show customer group name at Front Office (account page .etc) automatically.

For example, add group name which a specific customer belongs to after logged in:

Welcome, {$customerName} Log out. You are {GroupName}.

Here {GroupName} is what I am looking for. Please help me on this. How to get this cmd?

Millions thanks in advance.

Alex

Link to comment
Share on other sites

Topics merged

PrestaShop doesn't provide the customer's group name in the data it provides to templates, so you will need to add it. Add the following code to init.php after $cookie = new Cookie('ps');

if (intval($cookie->id_customer) > 0)
{
  $customer = new Customer(intval($cookie->id_customer));

  if (Validate::isLoadedObject($customer))
  {
     $group = new Group(intval($customer->id_default_group));

     if (Validate::isLoadedObject($group))
        $smarty->assign('groupName', $group->name[intval($cookie->id_lang)]);
  }
}



You should then be able to use {$groupName} in any TPL file.

  • Like 2
Link to comment
Share on other sites

Topics merged

PrestaShop doesn't provide the customer's group name in the data it provides to templates, so you will need to add it. Add the following code to init.php after $cookie = new Cookie('ps');

if (intval($cookie->id_customer) > 0)
{
  $customer = new Customer(intval($cookie->id_customer));

  if (Validate::isLoadedObject($customer))
  {
     $group = new Group(intval($customer->id_default_group));

     if (Validate::isLoadedObject($group))
        $smarty->assign('groupName' => $group->name);
  }
}



Ok, thanks Rocky, I will add this and will let you know if it work.
thanks again
Link to comment
Share on other sites

Hi Rocky,

it seems not work in my localhost shop. it shows HTTP 500 error after adding the code you provided. (back to normal when deleted the code)

I copy and paste it into init.php file. Nothing changed. Why it doesn't work?

is it because of my localhost reason?

Pls help me. thanks.

Link to comment
Share on other sites

  • 1 month later...

Hello there,

Sorry to wake up this solved topic,

I have used Rocky's method successfully on a 1.3 shop.

I would like to use it with Prestashop 1.4 alpha 3.

The init.php file is slightly different. I tried to insert the snippet of code as follows:


// Required for BO cookie path
$currentFileName = array_reverse(explode("/", $_SERVER['SCRIPT_NAME']));

$cookie = new Cookie('psAdmin', substr($_SERVER['SCRIPT_NAME'], strlen(__PS_BASE_URI__), -strlen($currentFileName['0'])));

if (intval($cookie->id_customer) > 0)
{
$customer = new Customer(intval($cookie->id_customer));

if (Validate::isLoadedObject($customer))
{
$group = new Group(intval($customer->id_default_group));

if (Validate::isLoadedObject($group))
$smarty->assign('groupName', $group->name[intval($cookie->id_lang)]);
}
}

if (isset($_GET['logout'])) {
$url_redirect = '';
$cookie->logout();
}


I must be missing something because my call from the product.tpl file with {$groupName} doesn't return anything.

Any suggestion would be much appreciated!
Link to comment
Share on other sites

Rocky, you are a legend!

It works thank you very much :-)

Just a note if somebody else wants to do the same things:

On the Frontcontroller.php file there are two lines with:

$cookie = new Cookie('ps');



Insert the code after the second line (line 119) and it should work :-)

Thanks again

Link to comment
Share on other sites

  • 2 months later...
Topics merged

PrestaShop doesn't provide the customer's group name in the data it provides to templates, so you will need to add it. Add the following code to init.php after $cookie = new Cookie('ps');

if (intval($cookie->id_customer) > 0)
{
  $customer = new Customer(intval($cookie->id_customer));

  if (Validate::isLoadedObject($customer))
  {
     $group = new Group(intval($customer->id_default_group));

     if (Validate::isLoadedObject($group))
        $smarty->assign('groupName', $group->name[intval($cookie->id_lang)]);
  }
}

Thanks a Lot! You code is perfect and works in my 1.3.6 Vr



You should then be able to use {$groupName} in any TPL file.

Link to comment
Share on other sites

  • 1 month later...
  • 4 months later...
  • 5 months later...
  • 4 months later...
  • 2 weeks later...

Here is a proper way to have customer groups in templates (tpl).

 

1. Create a file called FrontController.php in override/classes

2. Copy/paste the following code in this empty file

 

class FrontController extends FrontControllerCore
{
   public function init()
   {
       parent::init();
       self::$smarty->assign('customerGroups', parent::getCurrentCustomerGroups());
   }
}

 

3. You can now use {$customerGroups} in your templates

Edited by Daaaaad (see edit history)
  • Like 2
Link to comment
Share on other sites

  • 2 months later...
  • 4 weeks later...
Topics merged

 

PrestaShop doesn't provide the customer's group name in the data it provides to templates, so you will need to add it. Add the following code to init.php after $cookie = new Cookie('ps');

 

if (intval($cookie->id_customer) > 0)
{
$customer = new Customer(intval($cookie->id_customer));

if (Validate::isLoadedObject($customer))
{
$group = new Group(intval($customer->id_default_group));

if (Validate::isLoadedObject($group))
$smarty->assign('groupName', $group->name[intval($cookie->id_lang)]);
}
}

 

You should then be able to use {$groupName} in any TPL file.

 

Unfortunately it's not working in Version 1.2.4.0.

 

I don't see anything in FO. Do you have any ideas?

Edited by mhu100 (see edit history)
Link to comment
Share on other sites

  • 2 months later...

Just to verify, this works in Prestashop 1.5. Add Daaaaad's code to override/classes/controller FrontController.php.

 

It provided me the array of group id number(s) for the customer.

 

More details at http://melriks.com/p...certain-groups/

 

Hello,

 

How can you add Group Name in FO??

I have already add the code in "FrontController.php"

Then add "{$customerGroups}" in the .tpl files.

 

It shows "Array" on the page.

Could you please help?? Thanks.

Link to comment
Share on other sites

  • 2 months later...
  • 2 months later...

Hello,

 

How can you add Group Name in FO??

I have already add the code in "FrontController.php"

Then add "{$customerGroups}" in the .tpl files.

 

It shows "Array" on the page.

Could you please help?? Thanks.

 

After following the suggestion Daaaaad, I got a phrase "Array" too. Anybody knows how to fix?

 

PS 1.5.2

Edited by Pichayutm (see edit history)
Link to comment
Share on other sites

  • 10 months later...

Here is a proper way to have customer groups in templates (tpl).

 

1. Create a file called FrontController.php in override/classes

2. Copy/paste the following code in this empty file

 

class FrontController extends FrontControllerCore
{
    public function init()
    {
        parent::init();
        self::$smarty->assign('customerGroups', parent::getCurrentCustomerGroups());
    }
}
3. You can now use {$customerGroups} in your templates

 

 

 

work for 1.5.x version??????

Link to comment
Share on other sites

  • 10 months later...

It worked on 1.5, but does not work in 1.6.

 

Please review this thread as an alternative.

 

https://www.prestashop.com/forums/topic/228277-get-customer-group-id-in-smarty/

 

 

 

I tried this method as well, but it did not work.

 

https://www.prestashop.com/forums/topic/285808-solved-display-a-div-only-for-a-specific-user-group/

Edited by melriks (see edit history)
Link to comment
Share on other sites

×
×
  • Create New...