WordPress at Your Fingertips
function is not described

WC_Session_Handler::init_session_cookie() public WC 3.6.0

Setup cookie and customer ID.

{} It's a method of the class: WC_Session_Handler{}

No Hooks.

Return

null. Nothing.

Usage

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

Changelog

Since 3.6.0 Introduced.

Code of WC_Session_Handler::init_session_cookie() WC 5.9.0

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

	if ( $cookie ) {
		$this->_customer_id        = $cookie[0];
		$this->_session_expiration = $cookie[1];
		$this->_session_expiring   = $cookie[2];
		$this->_has_cookie         = true;
		$this->_data               = $this->get_session_data();

		// If the user logs in, update session.
		if ( is_user_logged_in() && strval( get_current_user_id() ) !== $this->_customer_id ) {
			$guest_session_id   = $this->_customer_id;
			$this->_customer_id = strval( get_current_user_id() );
			$this->_dirty       = true;
			$this->save_data( $guest_session_id );
			$this->set_customer_session_cookie( true );
		}

		// Update session if its close to expiring.
		if ( time() > $this->_session_expiring ) {
			$this->set_session_expiration();
			$this->update_session_timestamp( $this->_customer_id, $this->_session_expiration );
		}
	} else {
		$this->set_session_expiration();
		$this->_customer_id = $this->generate_customer_id();
		$this->_data        = $this->get_session_data();
	}
}