Automattic\WooCommerce\Internal\Caches
ProductVersionStringInvalidator::invalidate_products_with_term
Invalidate all products and variations that have a specific term assigned.
Uses the indexed wp_term_relationships table for efficient lookups. The list of entities associated with the term is cached for performance; the TTL can be customized via the woocommerce_version_string_invalidator_taxonomy_lookup_ttl
Method of the class: ProductVersionStringInvalidator{}
Hooks from the method
Returns
null. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->invalidate_products_with_term( $tt_id ): void;
- $tt_id(int) (required)
- The term taxonomy ID.
ProductVersionStringInvalidator::invalidate_products_with_term() ProductVersionStringInvalidator::invalidate products with term code WC 10.7.0
private function invalidate_products_with_term( int $tt_id ): void {
global $wpdb;
$cache_key = 'wc_cache_inv_term_' . $tt_id;
$entity_ids = wp_cache_get( $cache_key, 'woocommerce' );
if ( false === $entity_ids ) {
$entity_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT tr.object_id
FROM {$wpdb->term_relationships} tr
INNER JOIN {$wpdb->posts} p ON tr.object_id = p.ID
WHERE tr.term_taxonomy_id = %d
AND p.post_type IN ('product', 'product_variation')",
$tt_id
)
);
/**
* Filters the cache TTL for queries that find entities associated with a term or taxonomy.
*
* These queries are used during cache invalidation to determine which entities
* (e.g., products, variations) need their cache cleared when a term or attribute changes.
*
* @since 10.5.0
*
* @param int $ttl Cache TTL in seconds. Default 300 (5 minutes).
* @param string $entity_type The type of entity being invalidated ('product').
*/
$ttl = apply_filters( 'woocommerce_version_string_invalidator_taxonomy_lookup_ttl', self::DEFAULT_TAXONOMY_LOOKUP_CACHE_TTL, 'product' );
wp_cache_set( $cache_key, $entity_ids, 'woocommerce', $ttl );
}
foreach ( $entity_ids as $entity_id ) {
$post_type = get_post_type( (int) $entity_id );
if ( 'product_variation' === $post_type ) {
$this->invalidate_variation_and_parent( (int) $entity_id );
} else {
$this->invalidate( (int) $entity_id );
}
}
}