Automattic\WooCommerce\StoreApi\Utilities

ProductQuery::get_last_modifiedpublicWC 1.0

Get last modified date for all products as an HTTP-date (RFC 7232).

The result is cached in the 'wc_products' object cache group and invalidated via the clean_post_cache hook in WC_Post_Data::invalidate_products_last_modified().

Note: This intentionally does NOT use WordPress core's wp_cache_get_last_changed() / wp_cache_set_last_changed() pattern. Those functions are designed for opaque cache-key salting where auto-seeding with the current time on a cache miss is acceptable (a wrong salt simply causes a cache miss and re-query). Here, the value is exposed to clients via the Last-Modified HTTP header for collection cache invalidation. Auto-seeding with "now" on a cache miss would force all clients to unnecessarily invalidate their local caches. Instead, on a cache miss we fall back to the database to get the real last modification time and cache that.

Method of the class: ProductQuery{}

No Hooks.

Returns

String|null. HTTP-date formatted string, or null if no products exist.

Usage

$ProductQuery = new ProductQuery();
$ProductQuery->get_last_modified();

ProductQuery::get_last_modified() code WC 10.8.1

public function get_last_modified() {
	$last_modified = wp_cache_get( 'last_modified', 'wc_products' );

	if ( false === $last_modified ) {
		global $wpdb;

		$last_modified_gmt = $wpdb->get_var(
			"SELECT MAX( post_modified_gmt ) FROM {$wpdb->posts} WHERE post_type IN ( 'product', 'product_variation' )"
		);

		if ( ! $last_modified_gmt ) {
			return null;
		}

		$last_modified = gmdate( 'D, d M Y H:i:s', strtotime( $last_modified_gmt ) ) . ' GMT';
		wp_cache_set( 'last_modified', $last_modified, 'wc_products' );
	}

	return $last_modified;
}