WC_Session_Handler::destroy_session_if_empty │ public │ WC 10.3.0

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.

Метод класса: WC_Session_Handler{}

Хуков нет.

Возвращает

void. Ничего (null).

Использование

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

Список изменений

С версии 10.3.0 Введена.

Код WC_Session_Handler::destroy_session_if_empty() WC 11.2.2

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();
}