Peter PANC Posted March 23, 2022 Share Posted March 23, 2022 Hello everyone, I'm developing a module and it uses cookies to display a popup. I'm using if (isset($_COOKIE["panc_agepopupcookie"])) { } So, using core php to manage cookies info but Prestashop validator tells me to use: Context::getContext()->cookie Tried reading the PS Docs but couldn't find anything related to the use of cookies in modules... How can i read if cookie exists or not ont my module config page hookDisplayFooter() ? Thank you. Link to comment Share on other sites More sharing options...
knacky Posted March 25, 2022 Share Posted March 25, 2022 There are several ways. It also depends on the Prestashop version. Module Add new cookie: $this->context->cookie->__set('cookie_name', 'cookie value'); $this->context->cookie->write(); Module Read cookie: $this->context->cookie->__get('cookie_name'); Module Delete cookie: $this->context->cookie->__unset('cookie_name'); Module Check if cookie exists: $this->context->cookie->__isset('cookie_name'); Readt cookie in TPL: {$cookie->cookie_name} If you don’t have access to $this->context, you can replace it with Context::getContext(). Link to comment Share on other sites More sharing options...
knacky Posted March 25, 2022 Share Posted March 25, 2022 Or Module Create cookie: $cookie = new Cookie('cookie_name'); $cookie->setExpire(time() + 60 * 60); // The cookie expires in 1 hour. // $cookie->setExpire(0); // The cookie expires when the browser is closed $cookie->my_cookie_value = 'this is main cookie value'; $cookie->write(); Module Read cookie: $cookie = new Cookie('cookie_name'); echo $cookie->my_cookie_value; Read cookie in TPL: {$cookie->cookie_name} Link to comment Share on other sites More sharing options...
Peter PANC Posted March 25, 2022 Author Share Posted March 25, 2022 Thank you for your help, I'm setting the cookie using JS and the problem is reading the cookie in the module main file. This is what i wanna do: Only display the .tpl file if the cookie exists like this: if(isset(Context::getContext()->cookie->cookie_name_here)){ return $this->context->smarty->fetch($this->local_path.'views/templates/hooks/footer.tpl'); } And its not working. Link to comment Share on other sites More sharing options...
knacky Posted March 25, 2022 Share Posted March 25, 2022 (edited) You need to use the Ajax function in your module. That's not how it will work for you. Sample: https://www.google.com/amp/s/webkul.com/blog/ajax-url-call-controller-using-front-controller/amp/ https://stackoverflow.com/questions/42925427/call-a-prestashop-module-with-ajax Edited March 25, 2022 by knacky (see edit history) 1 Link to comment Share on other sites More sharing options...
Peter PANC Posted March 25, 2022 Author Share Posted March 25, 2022 Again, thank you for your help. It's strange that the module was working using $_COOKIE from php. It was working great this way. Why would prestashop force using the cookie object if it can't do the same stuff $_COOKIE did? Maybe i'm not explaining the way i should Imagine you have a simple "Cookie law" module. First time the user gets in the website the module is called because the cookie is not present. After the user accepts the terms, the cookie is set and the module doesnt show up again. if(isset(Context::getContext()->cookie->cookie_name_here)) { return $this->context->smarty->fetch($this->local_path.'views/templates/hooks/footer.tpl'); } Thats why i was trying to do that since it was working this way before if (isset($_COOKIE['my_cookie_name'])) { return $this->context->smarty->fetch($this->local_path.'views/templates/hooks/footer.tpl'); } Again, thank you so much knacky, your replies helped me learn new stuff 2 Link to comment Share on other sites More sharing options...
knacky Posted March 29, 2022 Share Posted March 29, 2022 Solved 😁 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now