Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

WooCommerce Category Count Fix

Fixes incorrect WooCommerce product category counts when moving subcategories between parents or deleting categories. Addresses WooCommerce issue #25491 — open since January 2020.

The Problem

When you move a subcategory from one parent to another in WooCommerce, the product counts on both parents become wrong:

  • Old parent keeps the inflated count (still includes the moved subcategory's products)
  • New parent keeps the deflated count (doesn't include the moved subcategory's products)

The same happens when deleting a category — the former parent's count goes stale.

How to Reproduce

  1. Create Category A with 5 products
  2. Create Category B (empty)
  3. Create Subcategory C under Category A with 3 products
  4. Category A shows count = 8 (5 own + 3 from C) ✓
  5. Move Subcategory C from Category A to Category B
  6. Category A still shows 8 ✗ — should be 5
  7. Category B still shows 0 ✗ — should be 3

This affects the product category widget, shop page counts, layered navigation, and any code relying on get_terms() with WooCommerce's term count override.

Root Cause

WooCommerce stores hierarchical product counts in wp_termmeta via _wc_term_recount(). This function recounts a category's products including all descendant categories.

The recount is triggered when products are assigned to or removed from categories (set_object_terms hook). But it is not triggered when:

  • A category's parent is changed (edited_product_cat)
  • A category is deleted (delete_product_cat)

So moving or deleting categories leaves the parent counts stale until a product is added or removed from those categories, or a manual recount is run.

How This Plugin Fixes It

Three hooks, one batched recount:

  1. edit_product_cat (pre-update) — Snapshots the category's current parent before the edit happens. This captures the "old" parent so we can recount it after the move.

  2. edited_product_cat (post-update) — Queues the edited category, its new parent chain, and its old parent chain for recount.

  3. delete_product_cat — Queues the deleted category's former parent chain for recount.

All queued term IDs are batched and recounted once on shutdown, so bulk edits, imports, and REST API batch operations don't trigger redundant queries.

The plugin uses WooCommerce's own _wc_term_recount() function — the same function WooCommerce already uses for product assignment recounts. This ensures the fix respects:

  • Hierarchical counting (parent includes children)
  • Visibility exclusions (hidden products, out-of-stock if configured)
  • Transient cache clearing (wc_term_counts)

Installation

As a Regular Plugin

  1. Download woocommerce-category-count-fix.php
  2. Upload to wp-content/plugins/woocommerce-category-count-fix/
  3. Activate in Plugins > Installed Plugins

As a Must-Use Plugin

  1. Download woocommerce-category-count-fix.php
  2. Upload to wp-content/mu-plugins/
  3. No activation needed — it loads automatically

Via WP-CLI

wp plugin install https://github.com/renzojohnson/woocommerce-category-count-fix/archive/refs/heads/main.zip --activate

After Installation

If your category counts are already wrong, run a one-time recount:

WooCommerce > Status > Tools > Recount terms

This recalculates all existing counts. Going forward, the plugin keeps them accurate automatically.

Logs

The plugin logs every recount operation to WooCommerce's logger:

[category-count-fix] Category counts recalculated for term_ids: 42, 15, 8

View logs at WooCommerce > Status > Logs and select category-count-fix from the source dropdown.

Requirements

  • WordPress 6.0+
  • WooCommerce 7.0+
  • PHP 8.1+

FAQ

Will this slow down my site?

No. The plugin only fires when a category is edited or deleted — not on every page load. Recounts are batched per request, so even bulk operations run a single recount query.

Does this work with HPOS?

Yes. This plugin operates on term metadata, not order storage. It works regardless of whether you use HPOS or the legacy post-based order storage.

Does this work with the REST API?

Yes. WordPress REST API term updates and deletions flow through the same core functions that fire edited_product_cat and delete_product_cat.

Does this work with WP-CLI?

Yes. WP-CLI term commands use the same core APIs and fire the same hooks.

What about WooCommerce's built-in "Recount terms" tool?

That tool does a one-time full recount. This plugin keeps counts accurate automatically so you don't need to run it manually after every category change.

Can I use this as a must-use plugin?

Yes. Upload the single PHP file to wp-content/mu-plugins/ and it works without activation. This is useful if you want to ensure the fix is always active regardless of plugin management.

Related

Author

Renzo Johnson — WordPress & WooCommerce developer.

Questions or feedback? Get in touch or open an issue on GitHub.

License

GPL-2.0-or-later

About

Fixes incorrect WooCommerce product category counts when moving subcategories between parents. Issue #25491 — open since 2020.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages