Untitled
webwizards
plain_text
9 months ago
1.1 kB
16
Indexable
add_action( 'wp', 'clear_variable_product_price_cache_on_load' );
function clear_variable_product_price_cache_on_load() {
// Check if we're on a single product page
if ( ! is_product() ) {
return;
}
// Get the current product
$product_id = get_the_ID();
$product = wc_get_product( $product_id );
if ( ! $product || ! $product->is_type( 'variable' ) ) {
return;
}
// Clear variable product price transients
delete_transient( 'wc_var_prices_' . $product_id );
// Clear the product children cache
delete_transient( 'wc_product_children_' . $product_id );
// Clear all product transients
wc_delete_product_transients( $product_id );
// Clear variation prices from cache
foreach ( $product->get_children() as $variation_id ) {
wp_cache_delete( $variation_id, 'post' );
delete_transient( 'wc_var_prices_' . $variation_id );
}
// Force the product to recalculate prices
$product->get_variation_prices( true ); // true = force refresh
}Editor is loading...
Leave a Comment