WC_Session_Handler::init_session_cookiepublicWC 3.6.0

Setup cookie and customer ID.

Method of the class: WC_Session_Handler{}

No Hooks.

Returns

null. Nothing (null).

Usage

$WC_Session_Handler = new WC_Session_Handler();
$WC_Session_Handler->init_session_cookie();

Changelog

Since 3.6.0 Introduced.

WC_Session_Handler::init_session_cookie() code WC 10.6.2

public function init_session_cookie() {
	$cookie = $this->get_session_cookie();

	if ( ! $cookie ) {
		// If there is no cookie, generate a new session/customer ID.
		$this->_customer_id = $this->generate_customer_id();
		$this->_data        = $this->get_session_data();
		return;
	}

	// Customer ID will be an MD5 hash id this is a guest session.
	$this->_customer_id        = $cookie[0];
	$this->_session_expiration = (int) $cookie[1];
	$this->_session_expiring   = (int) $cookie[2];
	$this->_has_cookie         = true;

	$this->restore_session_data();

	/**
	 * This clears the session if the cookie is invalid.
	 */
	if ( ! $this->is_session_cookie_valid() ) {
		$this->destroy_session();
	}

	// If the user logs in, update session.
	if ( is_user_logged_in() && (string) get_current_user_id() !== $this->get_customer_id() ) {
		$this->migrate_guest_session_to_user_session();
	}

	// Update session if its close to expiring.
	if ( $this->is_session_expiring() ) {
		$this->set_session_expiration();
		$this->update_session_timestamp( $this->get_customer_id(), $this->_session_expiration );
	}
}