Commit 57701f51d0 for woocommerce
commit 57701f51d0a71137ff7d12835a418010c8b73519
Author: Anand Rajaram <anandrajaram21@gmail.com>
Date: Mon Feb 9 14:09:45 2026 +0530
Analytics stock totals excludes trashed products (#63160)
* feat: update the get_count methods to calculate count with only post_status as publish
* feat: add hooks for trashing post to clear stock count cache
* Add changefile(s) from automation for the following project(s): woocommerce
* feat: update trashed_post, untrashed_post and delete_post hooks to conditionally clear stock count cache
---------
Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/63160-fix-analytics-trash b/plugins/woocommerce/changelog/63160-fix-analytics-trash
new file mode 100644
index 0000000000..2b1dd5c045
--- /dev/null
+++ b/plugins/woocommerce/changelog/63160-fix-analytics-trash
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fixes a bug with analytics stock totals that included trashed products
\ No newline at end of file
diff --git a/plugins/woocommerce/src/Admin/API/Reports/Stock/Stats/DataStore.php b/plugins/woocommerce/src/Admin/API/Reports/Stock/Stats/DataStore.php
index 67fe0bd9d1..98ada71f9c 100644
--- a/plugins/woocommerce/src/Admin/API/Reports/Stock/Stats/DataStore.php
+++ b/plugins/woocommerce/src/Admin/API/Reports/Stock/Stats/DataStore.php
@@ -80,6 +80,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON posts.ID = wc_product_meta_lookup.product_id
LEFT JOIN {$wpdb->postmeta} low_stock_amount_meta ON posts.ID = low_stock_amount_meta.post_id AND low_stock_amount_meta.meta_key = '_low_stock_amount'
WHERE posts.post_type IN ( 'product', 'product_variation' )
+ AND posts.post_status = 'publish'
AND wc_product_meta_lookup.stock_quantity IS NOT NULL
AND wc_product_meta_lookup.stock_status = 'instock'
AND (
@@ -119,6 +120,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
SELECT count( DISTINCT posts.ID ) FROM {$wpdb->posts} posts
LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON posts.ID = wc_product_meta_lookup.product_id
WHERE posts.post_type IN ( 'product', 'product_variation' )
+ AND posts.post_status = 'publish'
AND wc_product_meta_lookup.stock_status = %s
",
$status
diff --git a/plugins/woocommerce/src/Admin/ReportsSync.php b/plugins/woocommerce/src/Admin/ReportsSync.php
index 95d20d2a15..ff330c3d84 100644
--- a/plugins/woocommerce/src/Admin/ReportsSync.php
+++ b/plugins/woocommerce/src/Admin/ReportsSync.php
@@ -27,6 +27,9 @@ class ReportsSync {
add_action( 'woocommerce_new_product', array( __CLASS__, 'clear_stock_count_cache' ) );
add_action( 'update_option_woocommerce_notify_low_stock_amount', array( __CLASS__, 'clear_stock_count_cache' ) );
add_action( 'update_option_woocommerce_notify_no_stock_amount', array( __CLASS__, 'clear_stock_count_cache' ) );
+ add_action( 'trashed_post', array( __CLASS__, 'maybe_clear_stock_count_cache_for_post' ) );
+ add_action( 'untrashed_post', array( __CLASS__, 'maybe_clear_stock_count_cache_for_post' ) );
+ add_action( 'delete_post', array( __CLASS__, 'maybe_clear_stock_count_cache_for_post' ) );
}
/**
@@ -179,6 +182,22 @@ class ReportsSync {
return __( 'Report table data is being deleted.', 'woocommerce' );
}
+ /**
+ * Clear the stock count cache for a post if it's a product or product variation.
+ *
+ * Handles trashed_post, untrashed_post, and delete_post hooks.
+ *
+ * @internal
+ *
+ * @param int $post_id The post ID.
+ */
+ public static function maybe_clear_stock_count_cache_for_post( $post_id ): void {
+ $post = get_post( $post_id );
+ if ( $post && in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) {
+ self::clear_stock_count_cache( $post_id );
+ }
+ }
+
/**
* Clear the count cache when products are added or updated, or when
* the no/low stock options are changed.