2

I want to save temporary data (id and name) from product page to cart via Ajax. The problem is to record new variables in cookie, using outstanding from Joomla file. I tried:

$inputCookie  = JFactory::getApplication()->input->cookie;
$value        = $inputCookie->get($name = 'myCookie', $defaultValue = null);
$cookieExists = ($value !== null);
$inputCookie->set($name = 'myCookie', $value = '123', $expire = 0);

but in var_dump($_COOKIE) there is no 'myCoolie' variable with value.

And maybe for safety I need to use Session instead Cookie to keep products in cart?

2

The problem being your code will never work the first time cause your trying to set a cookie value based on the get value, which is always null as you're never setting the initial value.

To set a cookie, you can use the following:

$app = JFactory::getApplication();

// Get the cookie
$value = $app->input->cookie->get('myCookie', null);

// If there's no cookie value, manually set it
if ($value == null)
{
    $value = HOWEVER YOU GET THE CART DATA
}

// Set the cookie
$time = time() + 604800; // 1 week
$app->input->cookie->set('myCookie', $value, $time, $app->get('cookie_path', '/'), $app->get('cookie_domain'), $app->isSSLConnection());
0

This can help you.

https://stackoverflow.com/questions/16206662/how-to-use-cookies-from-a-component

use setvar and getString methods

JRequest::setVar($var, $value, 'cookie');
JRequest::getString($var, $default, 'cookie')

Or

https://api.joomla.org/cms-3/classes/Joomla.Input.Cookie.html

Or

http://blog.tormix.com/joomla/set-and-get-cookies-in-joomla-cms/

  • 2
    JRequest class is deprecated for few years and will be removed in J!4 – piotr_cz Mar 7 '17 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.