Are your WooCommerce product prices not updating immediately after changes? Here’s the quick fix:
The Solution: Clear WooCommerce Transients
To instantly reflect updated prices, use the following code:
For a Specific Product:
if ( function_exists( 'wc_delete_product_transients' ) ) {
wc_delete_product_transients( $product_id ); // Replace $product_id with your product's ID.
}
For All Products:
if ( function_exists( 'wc_delete_product_transients' ) ) {
wc_delete_product_transients();
}
That’s it! This clears WooCommerce’s cached data (transients), ensuring your updated prices are displayed immediately.
Why This Happens: WooCommerce Transients Explained
WooCommerce uses transients to cache product data for better performance. However, these cached values can delay showing updates, like new prices, on your website.
You can manually clear these transients by navigating to:
WooCommerce > Status > Tools > Clear Transients
But doing this repeatedly isn’t ideal. Instead, you can programmatically clear transients whenever necessary.
Automate Clearing Transients After Price Updates
To automate this process, add the following code to your plugin or theme’s functions.php
file:
add_action( 'woocommerce_product_object_updated_props', 'clear_transients_after_price_update' );
function clear_transients_after_price_update( $product ) {
if ( function_exists( 'wc_delete_product_transients' ) ) {
wc_delete_product_transients( $product->get_id() );
}
}
This ensures WooCommerce automatically clears the transients for a product whenever its details are updated, showing the new price instantly.
How WooCommerce Clears Transients
When you click Clear Transients in WooCommerce > Status > Tools, WooCommerce calls the following functions:
-
wc_delete_product_transients()
: Deletes product-related transients. -
delete_transient()
: A WordPress function to delete transients from the database. -
WC_Cache_Helper::delete_version_transients()
: Clears version-based transients.
Using wc_delete_product_transients()
programmatically mimics this process, but for individual or all products.
Why Clearing Transients Matters
Automating transient clearing ensures your product updates are reflected instantly, preventing customer confusion and improving the shopping experience. With this simple approach, you’ll never have to worry about outdated prices showing up on your site.
Top comments (0)