VirtueMart Forum

VirtueMart 2 + 3 + 4 => Frontend Modules => Topic started by: el02154 on December 20, 2020, 21:13:36 PM

Title: Store recently viewed produsts in cookie - not session
Post by: el02154 on December 20, 2020, 21:13:36 PM
Hello to all,

I hope all of you are healthy :) . I would like to ask if anyone has find a way to store recently viewed products in a cookie and not session. This requiest comes from a client that wants to be able to show recently viewed products for 5-10 days after a using comes back to site. The current function is to show the products until the session ends, after that it starts counting from zero.
Title: Re: Store recently viewed produsts in cookie - not session
Post by: GJC Web Design on December 21, 2020, 12:44:05 PM
the browsed products are added to the session in the shop function static public function addProductToRecent ($productId)
components\com_virtuemart\helpers\shopfunctionsf.php

I guess a plugin that stores that session object vmlastvisitedproductids to a cookie
Title: Re: Store recently viewed produsts in cookie - not session
Post by: el02154 on December 29, 2020, 20:27:32 PM
Thanks GJC, I will try it and come back if needed.
Title: Re: Store recently viewed produsts in cookie - not session
Post by: el02154 on January 06, 2021, 18:12:44 PM
Hello,

Happy new year to all. If I create a new function same to this but with the only difference that instead of session I will use cookie then how I can retrieve this cookie and show the products?
Title: Re: Store recently viewed produsts in cookie - not session
Post by: el02154 on January 06, 2021, 20:52:44 PM
In addition, I have added the following in addProductToRecent function:

      $inputCookie = JFactory::getApplication()->input->cookie;
      $inputCookie->set($name = 'vmlastvisitedproductids_cookie', $value = $products_ids, time() + (86400 * 30));

but when I try to retrieve this cookie it does not exist. Does anyone have an idea of the reason?
Title: Re: Store recently viewed produsts in cookie - not session
Post by: Studio 42 on January 07, 2021, 00:58:15 AM
$inputCookie = JFactory::getApplication()->input->cookie;
$inputCookie->set($name = 'vmlastvisitedproductids_cookie', $products_ids);
$inputCookie->getString('vmlastvisitedproductids_cookie');

see https://api.joomla.org/cms-3/classes/Joomla.Input.Cookie.html
I dont think that a timestamp can be set
You have to convert the cookie to a string and use getString()
Or if you set an array in the cookie use getArray()
Get without filters save a command (filter is az-_  only)
Title: Re: Store recently viewed produsts in cookie - not session
Post by: el02154 on January 07, 2021, 12:18:37 PM
Thanks for the answer. I managed to create cookie but the expiration time is the same as the creation one and is marked as deleted! Can you help on this?

UPDATE: I am trying to modify "addProductToRecent" function and use cookie instead of session but nothing happens. It is like I never create the cookie...
Title: Re: Store recently viewed produsts in cookie - not session
Post by: Studio 42 on January 07, 2021, 18:20:36 PM
please copy your code here.
I cannot know what you write wrong.