Set, get, delete and update WooCommerce Sessions
Learn deep knowledge of WooCommerce Sessions. Set, get, delete and update WooCommerce sessions. wp_woocommerce_session cookie is used to store data. You can change the expiry time and expiration time of sessions and also create a separate class to handle them if you are using them for multiple purposes.
Why use WC Sessions?
PHP Sessions are strictly prohibited in the development of WordPress plugins and themes. WooCommerce has created an alternate solution to handle it. They use Cookies and a Database table “wp_woocommerce_sessions” to store data of sessions. Sessions usually store data of cart and WooCommerce notices. As a developer, you can use them to set and get any type of data.
Initiate WooCommerce Sessions for Guest users
It is necessary to initiate the sessions for guest users. The session is by default empty and not undertaken for guest users until they add a product to the cart. When a product is added to the cart, the wp_woocommerce_session cookie is created and data is stored in the wp_woocommerce_sessions table.
// Initiate the WooCommerce Sessions manually for guest users.
add_action('woocommerce_init', array($this, '{ function-name }'));
// Add call back function for sessions.
public function { function-name }() {
if (is_user_logged_in() || is_admin()) {
return;
}
if (isset(WC()->session)) {
if (!WC()->session->has_session()) {
WC()->session->set_customer_session_cookie(true);
}
}
}
How to Check Whether Sessions are initiated or not
Click on the lock icon or info icon in the URL box before the URL, you can see the different cookies in the list. Find the cookie started with wp_woocommerce_session. if it is in the list of cookies, your sessions have been initiated successfully.
Set data in WooCommerce Sessions
// Set Session value.
WC()->session->set('{session-variable}', {value});
// For Example Code:
WC()->session->set( 'my-session', array( 1, 2, 3 ) );
Get Data in WooCommerce Sessions
// Get Session value.
WC()->session->get('{session-variable}');
// For Example Code:
WC()->session->get( 'my-session' );
Remove data from WooCommerce Sessions
// Empty session data.
WC()->session->set( 'my-session', null );
Update existing values of WooCommerce Sessions
// Add another values in Session.
// First Get values from session then push the new values in array and store it again in session.
${array-variable-1-name} = WC()->session->get( {session-variable} );
${array-variable-2-name} = array( {values} );
array_push( {array-variable-1-name} , {array-variable-2-name} );
WC()->session->set('{session-variable}', {array-variable-1-name} );
// For Example Code:
$existing_value = (array) WC()->session->get( 'my-session' );
$new_values = array( 4, 5, 6 );
array_push( $existing_value , $new_values );
WC()->session->set('my-session', $existing_value );
How to change the wp_woocommerce_session cookie expiry time?
By default WooCommerce sessions stores data for 48 hours but you can extend or reduce it by overriding the time by using the “wc_session_expiring” and “wc_session_expiration” filters hook.
wc_session_expiring:
wc_session_expiring filter hook is used to change the time to alert the system that sessions are about to timeout. By default, it is 47 hours still you can define a duration of a few minutes/hours less than the actual duration of the session. Suppose you have a time of 100 hours to expire a session so that the expiring time can be 98 or 99 hours.
// For Example set when the session is about to expire
add_filter( 'wc_session_expiring', 'woocommerce_cart_session_about_to_expire', 100, 1 );
function woocommerce_cart_session_about_to_expire( $session_time ) {
// return 99 hours 1 hours before expiry time (100 hours).
return 60 * 60 * 99;
}
wc_session_expiration:
wc_session_expiration hook uses to set the time to expire any session. By default, it is 48 hours still you can set any time of your preference.
// for example set when the session will expire.
add_filter( 'wc_session_expiration', 'woocommerce_cart_session_expires', 100, 1);
function woocommerce_cart_session_expires( $session_time ) {
// Return seconds of 100 hours.
return 60 * 60 * 100;
}
Create a class to handle WooCommerce Sessions
// Create a class to handle your sessions.
class Raja_Aman_Example{
public function __construct(){
add_action('woocommerce_init', [ $this, 'initiate_wc_sessions'] );
add_action( 'raja_aman_remove_session', [ $this, 'delete_wc_sessions' ] , 10 , 1 );
}
public function initiate_wc_sessions() {
if ( is_user_logged_in() || is_admin() ) {
return;
}
if ( isset( WC()->session ) ) {
if ( !WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie(true);
}
}
}
public function delete_wc_sessions( $session_key = '' ) {
if ( !empty( $session_key ) ) {
WC()->session->set( $session_key, null );
}
}
public function store_wc_sessions( $values = array( 1, 2, 3, 4, 5 ) ){
if ( empty( WC()->session->get( 'my-session' ) ) ) {
WC()->session->set( 'my-session', $values );
} else {
// Remains the previously stored values and insert new one
$values = WC()->session->get( 'my-session' );
$new_values = [ 10, 11, 12, 13, 14 ];
array_push( $values , $new_values );
WC()->session->set('my-session', $values );
}
// Use action hook like this to empty the session
do_action( 'raja_aman_remove_session' , 'my-session' );
// Or simply do it.
WC()->session->set( 'my-session', null );
}
}
WooCommerce classes to handle Sessions
Raja Aman Ullah is a highly skilled WordPress and WooCommerce plugin developer working as a lead plugin developer. He developed numerous famous extensions including b2b, request a quote, ultimate membership, custom fields, and many others for WooCommerce that are trending on Extension Store. His products on WooCommerce have more than 100,000 active subscriptions and happy clients.
Hire me
Excellent explanation of WooCommerce sessions and cookies.
Thanks for your valuable comment.