How to get Prestashop current user id?
Asked Answered
K

4

7

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

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

but its not working for me.. am using prestashop 1.5 ..

Kohl answered 26/4, 2013 at 7:58 Comment(3)
Can you give more details ? What are you trying to do in your php file ? Are you in front or back office ? Is your php file called with ajax ? We need the context to understand where the problem can come from.Nebiim
I already got the answer AlexDebKohl
blog.gofenice.com/uncategorized/get-current-user-id-prestashopGujral
G
12

I certainly couldn't get it to work in my test either. However, you can try

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

which works for me. I'm not at all sure that this is the best way to do it though.

Grating answered 26/4, 2013 at 20:32 Comment(0)
P
7

First check if user is logged in than get the id by $this->context->customer->id_customer

if ($this->context->customer->isLogged()) {

      echo $this->context->customer->id_customer;

}
else{
   echo 'Not LoggedIn';
}
Printmaking answered 3/7, 2013 at 11:17 Comment(0)
S
4

You shouldn't be using cookie.

Just use this:

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

you can remove (int), but i like to specify the type of content im getting.

BR's

Salpiglossis answered 14/1, 2014 at 8:3 Comment(0)
F
4

In Prestashop 1.6, the best way in a controller is to use :

        $id_customer = null;
        if ($this->context->customer->isLogged()) {
            // code to execute if i am logued
             $id_customer = $this->context->customer->id;
        }
Followup answered 23/7, 2014 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.