t $tt_id The term taxonomy ID. * * @return void */ 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 ); } } } /** * Invalidate all products using a specific attribute taxonomy. * * The list of entities associated with the taxonomy is cached for performance; * the TTL can be customized via the 'woocommerce_version_string_invalidator_taxonomy_lookup_ttl' filter. * * @param string $taxonomy The attribute taxonomy slug. * * @return void */ private function invalidate_products_with_attribute( string $taxonomy ): void { global $wpdb; $cache_key = 'wc_cache_inv_attr_' . $taxonomy; $cached = wp_cache_get( $cache_key, 'woocommerce' ); if ( false === $cached ) { $product_ids = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_product_attributes' AND meta_value LIKE %s", '%' . $wpdb->esc_like( 's:' . strlen( $taxonomy ) . ':"' . $taxonomy . '"' ) . '%' ) ); $variation_ids = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s", 'attribute_' . $taxonomy ) ); $cached = array( 'product_ids' => $product_ids, 'variation_ids' => $variation_ids, ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment -- Documented above. $ttl = apply_filters( 'woocommerce_version_string_invalidator_taxonomy_lookup_ttl', self::DEFAULT_TAXONOMY_LOOKUP_CACHE_TTL, 'product' ); wp_cache_set( $cache_key, $cached, 'woocommerce', $ttl ); } foreach ( $cached['product_ids'] as $product_id ) { $this->invalidate( (int) $product_id ); } foreach ( $cached['variation_ids'] as $variation_id ) { $this->invalidate_variation_and_parent( (int) $variation_id ); } } /** * Invalidate a product version string. * * @param int $product_id The product ID. * * @return void * * @since 10.5.0 */ public function invalidate( int $product_id ): void { wc_get_container()->get( VersionStringGenerator::class )->delete_version( "product_{$product_id}" ); } /** * Invalidate the product list version string. * * This should be called when products are created, deleted, or change status, * as these operations affect collection/list endpoints. * * @return void */ private function invalidate_products_list(): void { wc_get_container()->get( VersionStringGenerator::class )->delete_version( 'list_products' ); } /** * Invalidate the variations list version string for a specific product. * * This should be called when variations are created, deleted, or change status, * as these operations affect the variations collection/list endpoint for the parent product. * * @param int|null $product_id The parent product ID, or null/0 to skip invalidation. * * @return void */ private function invalidate_variations_list( ?int $product_id ): void { if ( $product_id ) { wc_get_container()->get( VersionStringGenerator::class )->delete_version( "list_product_variations_{$product_id}" ); } } }