How to Set/Unset WooCommerce Session Variables

Set/Unset woocommerce session variables

Do you want to know how to set or unset WooCommerce session variables? Generally, we use PHP’s built-in session handler and session_start(). So if you want to create a new session variable in WooCommerce, then you would obviously think of using PHP’s built-in session handler, but you might (at least, I did) land up with few issues. For example, I was developing a plugin for WooCommerce where I created a new session variable using $_SESSION in cart page and read the same in checkout page. But the result was an empty string. The ideal way to set/unset a session variable is to use WooCommerce’s session handler. This tutorial will explain how to Set/Unset WooCommerce session variables.

How to set a Session variable in WooCommerce?

Use WC()->session->set() method to set a session variable.

WC()->session->set('sess_variable_name', $data);

How to read a session variable in WooCommerce?

Use WC()->session->get() method to read a session variable.

WC()->session->get( 'sess_variable_name' );

How to Unset a session variable in WooCommerce?

You can either set a variable to null or just use WC()->session->__unset() method.

WC()->session->set( 'sess_variable_name', null );

(or)

WC()->session->__unset( 'sess_variable_name' );

However, using ‘__unset‘ method will really unset the variable.

Updated on September 2, 2017

Was this article helpful?

Related Articles

Comments

  1. I’m trying to create a session variable in shop page and then I show this variable values in checkout page.
    I read your article and wrote this code in woocommerce function page:

    add_action( ‘template_redirect’, ‘set_custom_data_wc_session’ );
    function set_custom_data_wc_session () {
    if ( isset( $_GET[‘konumu’] ) || isset( $_GET[‘masa_no’] ) ) {
    $konum = isset( $_GET[‘konumu’] ) ? esc_attr( $_GET[‘konumu’] ) : ”;
    $masa = isset( $_GET[‘masa_no’] ) ? esc_attr( $_GET[‘masa_no’] ) : ”;

    // Set the session data
    WC()->session->set( ‘custom_data’, array( ‘konum’ => $konum, ‘masa’ => $masa ) );

    }
    }

    // Show session variables in checkout page
    add_action(‘woocommerce_before_checkout_form’, ‘yeni_alanlar’);

    function yeni_alanlar($checkout){

    $data = WC()->session->get(‘custom_data’);

    echo ”;

    if( isset($data[‘konum’]) && ! empty($data[‘konum’]) && isset($data[‘masa’]) && ! empty($data[‘masa’]) ){
    echo ‘Konumunuz : ‘ . $data[‘konum’] .’Masanız : ‘ . $data[‘masa’] .”;
    }

    echo ”;
    }

    But this code working when user’s second visit. It is not working when first visit. Why is that? Can you guide me?

    1. Try initializing the session in the init hook. I mean, use init hook instead of template_redirect and initialize the session as set_customer_session_cookie( boolean $set ). This will set the session cookie on-demand, usually after adding an item to the cart. So like this – WC()->session->set_customer_session_cookie( true ); in function set_custom_data_wc_session? FYI, I haven’t tested this.

      1. First of all, Thank for your time and your help.
        I solved this issue like this;

        // Creating session variables
        add_action( ‘init’, ‘oturum_degiskeni_olustur’ );
        function oturum_degiskeni_olustur () {
        // Early initialize customer session
        if ( isset(WC()->session) && ! WC()->session->has_session() ) {
        WC()->session->set_customer_session_cookie( true );
        }

        if ( isset( $_GET[‘konumu’] ) && isset( $_GET[‘masa_no’] ) ) {
        $konum = isset( $_GET[‘konumu’] ) ? esc_attr( $_GET[‘konumu’] ) : ”;
        $masa = isset( $_GET[‘masa_no’] ) ? esc_attr( $_GET[‘masa_no’] ) : ”;

        // Set the session data
        WC()->session->set( ‘custom_data’, array( ‘konum’ => $konum, ‘masa’ => $masa ) );
        }
        }

Leave a Comment