Making woocommerce session cookie expiring on php session ending
-
I’m working on creating (maintaining actually) a plugin which allow users to create movies.
This plugin contains many pages. The previous developer used
$_SESSION
to store datas about the movie creation (images, sound, texts etc) during the process in order not to lose it on the way.When the movie is crafted, a virtual product is added to the cart for the created movie. At this moment, woocommerce creates a cookie to store the cart session.
The problem is that on one side, I now have a
$_SESSION
containing the datas to use to create the final movie once the user would pay for the order, and on the other hand a cart cookie which handles the cart. And both$_SESSION
and woocommerce cookie haven’t the same life time circle :$_SESSION
is 1440 seconds starting from the moment the user stops using the website, the woocommerce cookie has a 48 hours long lifetime.**My goal is to destroy woocommerce session if my
$_SESSION
is expired.**But many problems are appearing :
1 – When I want to test if
$_SESSION
, it is not returned at all in the debug console from phpstorm, whereas I’m able to print it out.
2 –Session_id()
never returns any ID whereas I’m able to get thePHPSESSID
cookie name and value.
3 – UsingWC_Session_handler
object to check if woocommerce session exists thanks tohas_session
method, returns false whereas awp_woocommerce_session
cookie exists.I’m using both xdebug in phpstorm and google chrome web dev inspector to check out my variables, but still there is something missing to me.
Here above, my piece of code which is fired on plugin init. I also tried to put it in wp-config.php but still the same :
// get the current session.gc_maxlifetime for debug
$session_lifetime = ini_get(‘session.gc_maxlifetime’);
// Check if a wp_woocommerce_session cookie exists
if (isset($_COOKIE)) {
// Get woocommerce cookie
foreach ($_COOKIE as $key => $value) {
if (strstr($key, ‘wp_woocommerce_session’)) {
// The cookie exists, store it as an array to use it later
$cookieId = $key;
$cookie[$cookieId] = explode(‘||’, $value);
}
}
// if no session id is defined, and a woocommerce cookie is,
// trash the woocommerce cookie and set a default session.gc_maxlifetime to 48h
// else use the current cookie expire time as default session.gc_maxlifetime (cookie expire – time)
if ( !session_id() && isset($cookie[$cookieId][1])) {
setcookie($cookieId, “”, time() – 48 * 3600);
$session_lifetime = 48 * 3600;
} elseif ( session_id() && isset($cookie[$cookieId][1])) {
$session_lifetime = $cookie[$cookieId][1] – time();
} else {
$session_lifetime = 48 * 3600;
}
}
// define the definitive session.gc_maxlifetime and start the session
ini_set(‘session.gc_maxlifetime’, $session_lifetime);
session_set_cookie_params($session_lifetime);
session_start();Thanks for helping me out
EDIT : I’m not allowed to create a cookie as it would be time consuming, budget matter…
- The topic ‘Making woocommerce session cookie expiring on php session ending’ is closed to new replies.