wc_delete_related_product_transients()WC 9.8.0

Deprecated since 10.1.0 This function is deprecated and will be removed in a future version.. It is no longer supported and may be removed in future releases. It is recommended to replace this function with the same one.

Delete all related products transients when a product is updated/created. This is necessary because changing one product affects all related products too.

No Hooks.

Returns

null. Nothing (null).

Usage

wc_delete_related_product_transients( $post_id );
$post_id(int) (required)
The product ID updated/created.

Changelog

Since 9.8.0 Introduced.
Deprecated since 10.1.0 This function is deprecated and will be removed in a future version.

wc_delete_related_product_transients() code WC 10.3.3

function wc_delete_related_product_transients( $post_id ) {
	wc_deprecated_function( 'wc_delete_related_product_transients', '10.1.0', 'This function is deprecated and will be removed in a future version.' );

	if ( ! is_numeric( $post_id ) ) {
		return;
	}

	$transient_name          = 'wc_related_' . $post_id;
	$old_transient           = get_transient( $transient_name );
	$old_related_product_ids = array();

	if ( is_array( $old_transient ) && ! empty( $old_transient ) ) {
		$old_related_product_ids = $old_transient[ array_key_first( $old_transient ) ];
	}

	// Delete current product transient so that it can be refreshed below.
	delete_transient( $transient_name );

	// Gets new related products and sets current product transient.
	$new_related_product_ids = wc_get_related_products( $post_id, 1000 );

	// Combine all product IDs that need their transients cleared.
	$related_product_ids = array_unique(
		array_merge(
			$old_related_product_ids,
			$new_related_product_ids
		)
	);

	if ( empty( $related_product_ids ) ) {
		return;
	}

	// Create the list of transient names to delete.
	$related_product_transients = array_map(
		function ( $id ) {
			return 'wc_related_' . $id;
		},
		$related_product_ids
	);
	_wc_delete_transients( $related_product_transients );
}