WC_Session_Handler::init_session_cookie()
Setup cookie and customer ID.
Method of the class: WC_Session_Handler{}
Hooks from the method
Return
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() WC Session Handler::init session cookie code WC 9.7.1
public function init_session_cookie() { $cookie = $this->get_session_cookie(); if ( $cookie ) { // Customer ID will be an MD5 hash id this is a guest session. $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 ( ! $this->is_session_cookie_valid() ) { $this->destroy_session(); $this->set_session_expiration(); } // 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 ); /** * Fires after a customer has logged in, and their guest session id has been * deleted with its data migrated to a customer id. * * This hook gives extensions the chance to connect the old session id to the * customer id, if the key is being used externally. * * @since 8.8.0 * * @param string $guest_session_id The former session ID, as generated by `::generate_customer_id()`. * @param int $customer_id The Customer ID that the former session was converted to. */ do_action( 'woocommerce_guest_session_to_user_id', $guest_session_id, $this->_customer_id ); } // 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(); } }