Automattic\WooCommerce\Internal\Caches

ProductVersionStringInvalidator::invalidate_products_with_attributeprivateWC 1.0

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

Method of the class: ProductVersionStringInvalidator{}

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->invalidate_products_with_attribute( $taxonomy ): void;
$taxonomy(string) (required)
The attribute taxonomy slug.

ProductVersionStringInvalidator::invalidate_products_with_attribute() code WC 10.7.0

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