I would prefer not installing a plugin to achieve this and I don't mind coding something myself. I'm simply unsure of the "wordpress-y" way to go about it.

So here goes, Each one of the products has four variations, always the same, they are the region selection and the prices are different based on those.

However, I would like to have a dropdown in the main nav of the site where customers can specify what variation (region) price is displayed across the site for the whole duration of their session.

So it's a two-part thing, I would like to know the ideal method of saving this user preference in the $_SESSION (I'm assuming) and then retrieving this information when displaying prices by variation, I already have a hook to handle that in the category pages but at the moment it defaults to just a hardcoded region, instead it should be user preference OR default of america.

This information does not need to be persisted to disk or DB, it can just be in the session.

Thank you in advance, and please let me know if anything needs clarification.

/edit Just noticed the wc offtopic tag, but although yes it's somewhat related to woocommerce, storing a value in the user session isn't a wc specific thing at all. It's a user session question, wc is just framing for the use case.

  • Most WP hosts do not support $_SESSION, it also isn't a way to get around cookies as the session ID is stored in a cookie, which is also a security issue. WordPress itself does not use PHP sessions.
    – Tom J Nowell
    Feb 13 at 18:05
  • @TomJNowell Ok no problem, I really have no attachment to $_SESSION either way. What is the mechanism I would use then? Clearly wordpress has a way of keeping track of users and their information.
    – LAZ
    Feb 13 at 21:17
  • The same way every other site does, cookies! Users sessions in WP are cookie based. Think about it, how does a site that's not built with PHP do it if there are no PHP sessions
    – Tom J Nowell
    Feb 14 at 1:58
  • @TomJNowell Yes of course, but are there really no pre-defined places (via cookies or not) that let me keep track of what guest/user has selected? Like cart for example, I know I don't directly edit the cookies, I would use built in WC methods for that. Is there something similar for navigation state / menus?
    – LAZ
    Feb 14 at 12:32
  • you'd need to create/update/read that cookie yourself, in JS/PHP/both. If WC provides a mechanism that's great but you would need to ask in a WC forum or community or via their official support as 3rd party plugin dev support is off-topic here. WP provides no state management for cookies/sessions other than are you or are you not logged in, the closest is user meta for logged in users. The part of your Q where you change the variations/prices based on the cookie is definitely off-topic and outside this stacks scope. I wouldn't build this into a WP nav menu
    – Tom J Nowell
    Feb 14 at 12:54


Your Answer

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

Browse other questions tagged or ask your own question.