How to get Prestashop current user id?

本秂侑毒 提交于 2019-12-22 05:31:19

问题


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 ..


回答1:


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.




回答2:


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';
}



回答3:


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




回答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;
        }


来源:https://stackoverflow.com/questions/16231440/how-to-get-prestashop-current-user-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!