How to Really Refresh the Cache in WooCommerce Analytics
If you’re using WooCommerce Analytics on your store and, for example, creating orders programmatically, you may notice, that the analytics data isn’t always updated when an order is created. I personally first noticed this issue when one of the customers who is using my order sync plugin created a support ticket about it (but now my plugin refreshes the cache correctly).
It is because there are multiple types of cache in use and it will take a while for your order to appear in WooCommerce Analytics reports.
In this tutorial we’re going to figure out what types of cache there are and how to properly clean them up (preferably programmatically).
WooCommerce Analytics Configuration
Do you agree that we can refresh the cache as many times as we can, but analytics won’t be updated if we’re trying to find an order with a status that is excluded in the WooCommerce analytics settings?
Excluded statuses
So, first of all, let’s go to the Analytics > Settings page and make sure we’re allowing the statuses we need here:

Date type
The “Excluded statuses” option is not the only thing that requires your attention on this analytics settings page.

The fun stuff is that by default, this option is set to “Date paid” value, but it means that all the orders that don’t have a “date paid” parameter in the metadata won’t appear in analytics reports!
Created an order programmatically but haven’t used the set_date_paid() method? Forget about your order appearing in the analytics reports.
wp_wc_order_stats Database Table
As you may notice, we’re still not talking about actually clearing the analytics cache. It is because what is the point of clearing the cache if the analytics isn’t configured the proper way?
The next thing I want to discuss is the wp_wc_order_stats database table, or, {$wpdb->prefix}wc_order_stats table, to be correct.
All the orders you see in your WooCommerce Analytics reports come from this table.
So, if something is off and WooCommerce Analytics is not updating the way you need it, first of all, I’d recommend you checking that table.
I have also seen a couple of recommendations on other websites that you might need to truncate this table in order to refresh the analytics cache, which is weird because, in that case, nothing is going to be displayed in your reports at all.
You can sync all the orders from the Analytics > Settings page as well using the “Import historical data” tool, though its description, “This tool populates historical analytics data…” doesn’t help at all. But now you know, it populates and refreshes the data in the wp_wc_order_stats table.

You can also sync a specific order by its ID programmatically, like this:
use Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore;
// of course it is not necessary to create a wrapping function, just an example
function rudr_refresh_order_cache( $order_id ) {
OrdersStatsDataStore::sync_order( $order_id );
}But usually, you don’t need to do it, because it is going to be scheduled to sync automatically on woocommerce_update_order and woocommerce_create_order hooks.
Transient Cache
Finally, we’re talking about the cache! 😁
The whole idea is that WooCommerce analytics reports are cached with transients (WordPress transient cache), and when we make some changes in orders, for example, we need to make sure that we don’t bypass this cache. In other words, it should be refreshed properly.
You can actually do it in WooCommerce > Status > Tools:

Or you can do it programmatically this way:
use Automattic\WooCommerce\Admin\API\Reports\Cache;
function rudr_clear_analytics_cache() {
Cache::invalidate();
}But the most important thing you need to keep in mind is that you need to clear the cache after the order is synced into the wp_wc_order_stats table. Remember, I said that orders are usually synced with scheduled actions? 🙃 Anyway, you can always check how this functionality is implemented in my order sync plugin.
Misha Rudrastyh
Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Hello,
Thanks for sharing this.
I was stuck when the manual orders I was creating was not showing.
Setting the date to date_created then using
Cache::invalidate();solved it.This was helpful.
Thank you