How to get the current Prestashop user ID?

I used the code below to try to get the current user id in prestashop. I put this code in another php file in my module directory and call it through the module file.

$id = $this->context->customer->id_customer; 

but it does not work for me .. i am using prestashop 1.5 ..

+6
source share
4 answers

I also could not get it to work in my test. However you can try

 $id = (int)$this->context->cookie->id_customer; 

which works for me. I'm not sure if this is the best way to do this though.

+12
source

First check if the user is registered, than get the identifier $this->context->customer->id_customer

 if ($this->context->customer->isLogged()) { echo $this->context->customer->id_customer; } else{ echo 'Not LoggedIn'; } 
+7
source

You cannot use cookies.

Just use this:

  $id=(int)$this->context->customer->id; 

you can remove (int), but I like to specify the content type im get.

BR's

+3
source

In Prestashop 1.6, it is best to use a controller:

  $id_customer = null; if ($this->context->customer->isLogged()) { // code to execute if i am logued $id_customer = $this->context->customer->id; } 
+3
source

Source: https://habr.com/ru/post/943682/


All Articles