-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Product Filters: Add get_taxonomy_counts
method to FilterData
class
#59462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Product Filters: Add get_taxonomy_counts
method to FilterData
class
#59462
Conversation
Adds the method to the class to calculate product counts for a given taxonomy. This is used by the product filter blocks to show the number of products for each filter option.
Testing GuidelinesHi @Aljullu , Apart from reviewing the code changes, please make sure to review the testing instructions (Guide) and verify that relevant tests (E2E, Unit, Integration, etc.) have been added or updated as needed. Reminder: PR reviewers are required to document testing performed. This includes:
|
47f2675
to
00d2de2
Compare
📝 WalkthroughWalkthroughA new Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant FilterData
participant WordPress
participant Database
Client->>FilterData: get_taxonomy_counts(query_vars, taxonomy)
FilterData->>WordPress: Apply 'woocommerce_pre_product_filter_data' filter
alt Pre-filter returns array
FilterData-->>Client: Return pre-filtered counts
else No pre-filter result
FilterData->>WordPress: Check transient cache
alt Cache hit
FilterData-->>Client: Return cached counts
else Cache miss
FilterData->>WordPress: Get matching product IDs (WP_Query)
FilterData->>Database: SQL query for term counts (term_relationships, term_taxonomy)
Database-->>FilterData: Raw term counts
FilterData->>WordPress: Apply 'woocommerce_product_filter_data' filter
FilterData->>WordPress: Set transient cache
FilterData-->>Client: Return calculated counts
end
end
📜 Recent review detailsConfiguration used: .coderabbit.yml 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (3)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
plugins/woocommerce/changelog/wooplug-4765-add-filterdataget_taxonomy_counts-method-to-calculate-the (1)
4-5
: Complete the changelog description.The changelog description appears to be incomplete. Line 4 ends with "for efficient" and line 5 is empty, suggesting the description was cut off mid-sentence.
Complete the description to properly explain what the method does:
-Product Filters: add `get_taxonomy_counts` method to FilterData class for efficient +Product Filters: add `get_taxonomy_counts` method to FilterData class for efficient calculation of taxonomy term counts
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
plugins/woocommerce/changelog/wooplug-4765-add-filterdataget_taxonomy_counts-method-to-calculate-the
(1 hunks)plugins/woocommerce/src/Internal/ProductFilters/FilterData.php
(1 hunks)plugins/woocommerce/tests/php/src/Internal/ProductFilters/AbstractProductFiltersTest.php
(5 hunks)plugins/woocommerce/tests/php/src/Internal/ProductFilters/FilterDataTest.php
(2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`plugins/woocommerce/tests/**/*.php`: Run WooCommerce PHPUnit tests for specific...
plugins/woocommerce/tests/**/*.php
: Run WooCommerce PHPUnit tests for specific files or directories using the command: pnpm run test:php:env {relative_path} --verbose, and ensure the command is run in the plugins/woocommerce directory.
📄 Source: CodeRabbit Inference Engine (.cursor/rules/woo-phpunit.mdc)
List of files the instruction was applied to:
plugins/woocommerce/tests/php/src/Internal/ProductFilters/AbstractProductFiltersTest.php
plugins/woocommerce/tests/php/src/Internal/ProductFilters/FilterDataTest.php
🧠 Learnings (1)
plugins/woocommerce/tests/php/src/Internal/ProductFilters/AbstractProductFiltersTest.php (2)
Learnt from: gigitux
PR: woocommerce/woocommerce#58846
File: plugins/woocommerce/client/blocks/tests/e2e/tests/all-products/all-products.block_theme.spec.ts:41-52
Timestamp: 2025-06-16T09:20:22.981Z
Learning: In WooCommerce E2E tests, the database is reset to the initial state for each test, so there's no need to manually restore global template changes (like clearing the header template) as the test infrastructure handles cleanup automatically.
Learnt from: mreishus
PR: woocommerce/woocommerce#58817
File: plugins/woocommerce/includes/wc-product-functions.php:140-140
Timestamp: 2025-06-13T23:52:46.221Z
Learning: In WooCommerce's wc_delete_product_transients function, the context check includes both $is_admin_page (for regular admin screens) and $is_privileged_ajax (for AJAX requests from users with edit_products capability), ensuring that legitimate admin AJAX operations like product imports/exports can still delete transients.
🧬 Code Graph Analysis (3)
plugins/woocommerce/tests/php/src/Internal/ProductFilters/AbstractProductFiltersTest.php (1)
plugins/woocommerce/src/Enums/ProductStockStatus.php (1)
ProductStockStatus
(10-38)
plugins/woocommerce/src/Internal/ProductFilters/FilterData.php (1)
plugins/woocommerce/includes/wc-formatting-functions.php (1)
wc_sanitize_taxonomy_name
(64-66)
plugins/woocommerce/tests/php/src/Internal/ProductFilters/FilterDataTest.php (1)
plugins/woocommerce/src/Internal/ProductFilters/FilterData.php (1)
get_taxonomy_counts
(297-349)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (14)
- GitHub Check: Metrics - @woocommerce/plugin-woocommerce [performance]
- GitHub Check: Core API tests - @woocommerce/plugin-woocommerce [api]
- GitHub Check: PHP: 8.4 WP: latest [WP latest] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
- GitHub Check: Core e2e tests 5/6 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Core e2e tests 6/6 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Core e2e tests 3/6 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Core e2e tests 4/6 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Core e2e tests 2/6 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Core e2e tests 1/6 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.2] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
- GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.2] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
- GitHub Check: PHP: 8.4 WP: latest [WP latest] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
- GitHub Check: Lint - @woocommerce/plugin-woocommerce
- GitHub Check: build
🔇 Additional comments (15)
plugins/woocommerce/tests/php/src/Internal/ProductFilters/AbstractProductFiltersTest.php (5)
36-41
: LGTM: Property addition follows established patterns.The new
$product_categories
property is properly documented and follows the same pattern as other test properties in the class.
90-94
: LGTM: Category setup is well-structured.The category creation uses the existing
FixtureData
helper and stores the results in a logical key-value structure for easy reference in product assignments.
101-101
: LGTM: Product category assignments are consistent.The category assignments properly use the term IDs from the created categories and follow logical groupings:
- Products 1, 4, 5, 6 assigned to cat-1
- Products 2, 3 assigned to cat-2
- Product 3 also assigned to cat-3 (testing multi-category assignment)
Also applies to: 107-107, 113-116, 122-122, 127-127, 152-152
190-190
: LGTM: Cleanup integration follows established pattern.The addition of category cleanup in
tearDown()
follows the same pattern as existing cleanup calls.
260-267
: LGTM: Cleanup method is properly implemented.The
remove_all_product_categories()
method follows the established pattern of other cleanup methods in the class. It properly iterates through the created categories and deletes them from the correct taxonomy.plugins/woocommerce/src/Internal/ProductFilters/FilterData.php (6)
290-296
: LGTM: Method signature and documentation are consistent.The method signature, parameter types, and return type documentation follow the same pattern as
get_attribute_counts()
. The parameter naming is clear and follows conventions.
297-305
: LGTM: Pre-filter hook implementation is consistent.The pre-filter hook implementation follows the exact same pattern as other count methods, using the appropriate filter type 'taxonomy' and passing the taxonomy name in the extra parameters.
307-312
: LGTM: Caching implementation follows established pattern.The transient key generation and cache retrieval logic is identical to other count methods, ensuring consistent caching behavior across the class.
320-328
: LGTM: SQL query is well-constructed and secure.The SQL query properly:
- Uses
esc_sql()
andwc_sanitize_taxonomy_name()
for security- Joins the correct tables (
term_relationships
andterm_taxonomy
)- Counts distinct object_id (products) per term_taxonomy_id
- Groups by term_taxonomy_id as expected
The query structure is appropriate for counting taxonomy term usage.
336-338
: LGTM: Result processing follows established pattern.The result processing using
wp_list_pluck()
andarray_map('absint', ...)
is identical to theget_attribute_counts()
method, ensuring consistent data formatting.
341-348
: LGTM: Post-filter hook and caching are consistent.The post-filter hook and cache storage follow the exact same pattern as other count methods, ensuring consistent behavior and proper result filtering/caching.
plugins/woocommerce/tests/php/src/Internal/ProductFilters/FilterDataTest.php (4)
269-279
: LGTM: Default query test follows established pattern.The test method follows the same structure as
test_get_attribute_counts_with_default_query()
, properly testing the basic functionality of the new method with appropriate assertions.
281-301
: LGTM: Filtered query test provides good coverage.The test with max_price filter follows the same pattern as the attribute count tests, using a filter callback to determine which products should be included. This ensures the new method works correctly with query filters.
344-375
: LGTM: Helper method correctly calculates expected counts.The
get_expected_category_counts()
helper method:
- Follows the same pattern as
get_expected_attribute_counts()
- Properly handles the optional filter callback
- Correctly iterates through product data and category assignments
- Returns the expected format (term_id => count mapping)
This ensures test assertions have accurate expected values to compare against.
269-301
: Verify PHPUnit tests for new taxonomy-count functionalityPlease confirm that the two new tests in FilterDataTest pass locally before merging:
• test_get_taxonomy_counts_with_default_query
• test_get_taxonomy_counts_with_max_priceEnsure all plugin dependencies are installed, then run one of the following from the repository root:
– Using pnpm/wp-env
cd plugins/woocommerce pnpm install pnpm run test:php:env tests/php/src/Internal/ProductFilters/FilterDataTest.php --verbose
If you get “wp-env: not found,” install or link
@wordpress/env
(e.g.npm install -g @wordpress/env
).– Using Composer/Phar PHPUnit
cd plugins/woocommerce composer install vendor/bin/phpunit -c phpunit.xml --verbose tests/php/src/Internal/ProductFilters/FilterDataTest.php
Confirm both tests pass without errors.
Test using WordPress PlaygroundThe changes in this pull request can be previewed and tested using a WordPress Playground instance. Test this pull request with WordPress Playground. Note that this URL is valid for 30 days from when this comment was last updated. You can update it by closing/reopening the PR or pushing a new commit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b5605e4
to
3e5d613
Compare
Submission Review Guidelines:
Changes proposed in this Pull Request:
Add
get_taxonomy_counts
method toFilterData
class for calculating taxonomy term counts in product filters.Closes #59129 (WOOPLUG-4765)
Screenshots or screen recordings:
This PR does not include UI changes.
How to test the changes in this Pull Request:
Applied the following snippet and go to Shop/Category/filtered Shop page. Manual verify that the count matches current results.