WC_Session_Handler::destroy_session_if_empty
Destroys the WooCommerce session if it contains no data for non-logged-in users.
This method helps improve caching performance by removing session cookies when they are no longer needed, allowing non-logged-in customers to receive cached pages. Only runs if the destroy-empty-sessions feature is enabled.
Method of the class: WC_Session_Handler{}
No Hooks.
Returns
null. Nothing (null).
Usage
$WC_Session_Handler = new WC_Session_Handler(); $WC_Session_Handler->destroy_session_if_empty();
Changelog
| Since 10.3.0 | Introduced. |
WC_Session_Handler::destroy_session_if_empty() WC Session Handler::destroy session if empty code WC 10.3.3
public function destroy_session_if_empty() {
if ( is_user_logged_in() || ! $this->_has_cookie ) {
return;
}
if ( ! isset( $_COOKIE[ $this->_cookie ] ) ) {
// If $_COOKIE isn't set, then something triggered setting the cookie during this request. So we won't
// yet destroy the session if it is empty to expand compatibility at the cost of one additional request being uncached.
return;
}
if ( ! empty( $this->_data ) ) {
return;
}
if ( is_object( WC()->cart ) && ! WC()->cart->is_empty() ) {
// There is a pending cart to save that isn't yet in the session data.
return;
}
$feature_controller = wc_get_container()->get( FeaturesController::class );
if ( ! $feature_controller->feature_is_enabled( 'destroy-empty-sessions' ) ) {
return;
}
$this->destroy_session();
}