wc_update_user_last_active()WC 3.4.0

Set the user last active timestamp to now.

Returns

null. Nothing (null).

Usage

wc_update_user_last_active( $user_id );
$user_id(int) (required)
User ID to mark active.

Changelog

Since 3.4.0 Introduced.

wc_update_user_last_active() code WC 10.7.0

function wc_update_user_last_active( $user_id ) {
	if ( $user_id ) {
		// Optimization note: meta write invalidates caches and triggers repopulating them, hence guarded updates.
		// Default threshold: order placement, automation - guard against repetitive updates in short period of time.
		$threshold = MINUTE_IN_SECONDS;
		if ( doing_action( 'wp_login' ) ) {
			// When user logged in, execute update right away.
			$threshold = 0;
		} elseif ( doing_action( 'wp' ) ) {
			// When navigating the store pages, track online presence (5 minutes works for this sort of tracking).
			$threshold = 5 * MINUTE_IN_SECONDS;
		}

		/**
		 * Enables customization of the update frequency for user last active meta by adjusting the threshold from default to custom values.
		 * Note that a lower threshold results in more frequent database writes by Woo core and, if enabled, Analytics.
		 *
		 * @param int $threshold The threshold in seconds to be applied (1 minute, 5 minutes and 0 for immediate update).
		 * @return int
		 *
		 * @since 10.7.0
		 */
		$threshold = (int) apply_filters( 'woocommerce_update_user_last_active_threshold', $threshold );

		$now         = time();
		$last_active = get_user_meta( $user_id, 'wc_last_active', true );
		if ( ! $last_active || ( $now - $last_active ) > $threshold ) {
			update_user_meta( $user_id, 'wc_last_active', (string) $now, (string) $last_active );
		}
	}
}