Commit 02d842afc4d for woocommerce
commit 02d842afc4da1dda30a9a0e06bd6dde36fe37af9
Author: SH Sajal Chowdhury <72102985+shsajalchowdhury@users.noreply.github.com>
Date: Fri Jun 5 15:31:58 2026 +0600
Add woocommerce_term_recount_product_count filter to _wc_term_recount (#64374)
* Add woocommerce_term_recount_product_count filter to _wc_term_recount
Adds a filter to the product count value inside _wc_term_recount(), allowing
developers to adjust term counts with custom logic without extra queries or
overriding get_terms.
Fixes #34065
* Apply absint() to filtered count before storing in term meta
CodeRabbit suggestion: re-apply absint() after the filter to guard against
third-party filters returning non-numeric values.
* Add changelog entry
* Address reviewer feedback: update @since to 10.9.0, pass taxonomy object to filter, sanitize count
* Address reviewer feedback: use minor significance, fix WP_Taxonomy doc type
* Address review feedback: add absint() for $term_id, fix docblock alignment
---------
Co-authored-by: Vladimir Reznichenko <kalessil@gmail.com>
diff --git a/plugins/woocommerce/changelog/pr-64374 b/plugins/woocommerce/changelog/pr-64374
new file mode 100644
index 00000000000..07892b1f165
--- /dev/null
+++ b/plugins/woocommerce/changelog/pr-64374
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Add woocommerce_term_recount_product_count filter to _wc_term_recount.
diff --git a/plugins/woocommerce/includes/wc-term-functions.php b/plugins/woocommerce/includes/wc-term-functions.php
index 20e44c8ba43..fc66d4da6c5 100644
--- a/plugins/woocommerce/includes/wc-term-functions.php
+++ b/plugins/woocommerce/includes/wc-term-functions.php
@@ -525,7 +525,20 @@ function _wc_term_recount( $terms, $taxonomy, $callback = true, $terms_are_term_
// Get the count.
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
- $count = $wpdb->get_var( implode( ' ', $term_query ) );
+ $count = absint( $wpdb->get_var( implode( ' ', $term_query ) ) );
+
+ $term_id = absint( $term_id );
+
+ /**
+ * Filter the product count for a term before it is saved.
+ *
+ * @since 10.9.0
+ *
+ * @param int $count The product count for the term.
+ * @param int $term_id The term ID.
+ * @param WP_Taxonomy $taxonomy The taxonomy object.
+ */
+ $count = apply_filters( 'woocommerce_term_recount_product_count', $count, $term_id, $taxonomy );
// Update the count.
update_term_meta( $term_id, 'product_count_' . $taxonomy->name, absint( $count ) );