Jump to content

Using cookies in module | Module validator error (The use of $_COOKIE is forbidden; use Context::getContext()->cookie instead)


Recommended Posts

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

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

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

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

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 :)

  • Like 2
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...