Author Topic: Store recently viewed produsts in cookie - not session  (Read 590 times)

el02154

  • Beginner
  • *
  • Posts: 19
Store recently viewed produsts in cookie - not session
« 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.

GJC Web Design

  • 3rd party VirtueMart Developer
  • Super Hero
  • *
  • Posts: 10029
  • Virtuemart, Joomla & php developer
    • GJC Web Design
  • VirtueMart Version: 3.6.8
Re: Store recently viewed produsts in cookie - not session
« Reply #1 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
GJC Web Design
VirtueMart and Joomla Developers - php developers http://www.gjcwebdesign.com
VM3 AusPost Shipping Plugin - e-go Shipping Plugin - VM3 Postcode Shipping Plugin - Radius Shipping Plugin - VM3 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
http://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

el02154

  • Beginner
  • *
  • Posts: 19
Re: Store recently viewed produsts in cookie - not session
« Reply #2 on: December 29, 2020, 20:27:32 pm »
Thanks GJC, I will try it and come back if needed.

el02154

  • Beginner
  • *
  • Posts: 19
Re: Store recently viewed produsts in cookie - not session
« Reply #3 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?

el02154

  • Beginner
  • *
  • Posts: 19
Re: Store recently viewed produsts in cookie - not session
« Reply #4 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?

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4406
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: Store recently viewed produsts in cookie - not session
« Reply #5 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)

el02154

  • Beginner
  • *
  • Posts: 19
Re: Store recently viewed produsts in cookie - not session
« Reply #6 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...

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4406
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: Store recently viewed produsts in cookie - not session
« Reply #7 on: January 07, 2021, 18:20:36 pm »
please copy your code here.
I cannot know what you write wrong.