Skip to content
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

[Experimental] Add: QueryFilters class to calculate filter data for product queries #42811

Merged
merged 20 commits into from Jan 1, 2024

Conversation

dinhtungdu
Copy link
Member

@dinhtungdu dinhtungdu commented Dec 14, 2023

Submission Review Guidelines:

Changes proposed in this Pull Request:

Closes #42109.

This PR introduces a new QueryFilters class to calculate the filter data for product queries. The logic was brought over mainly from the StoreAPI/ProductQueryFilters class. Instead of making internal REST API requests to Store API, we use the new class to calculate the filter data for each request.

Unlike the WC_Query/Filterer (which only work with main query) and ProductQueryFilter (which only works with Store API requests), the new QueryFilters class has some public methods that can be used to calculate the filter data from WP_Query arguments. It only cares about the data, not where it comes from:

  • This paves the way to refactor the Store API Routes and WC_Query to use the new class.
  • We generalize and treat both query type exactly the same as they're both WC_Query under the hood.
  • Being able to passing custom query vars, we're adding support for both global and local query for Product Collection block. New filter blocks now work both inside and outside the Product Collection.
  • The QueryFilters can work with any product query, so we can now filter any product archive pages, even the search results page.

Beside the new class, this PR:

  • removes the meta query filter from ClassicTemplate class.
  • handles the stock filter in the new QueryFilters class using posts clauses instead of meta query.
  • updates CollectionFilters class to remove now unused and problematic query transformer, replaces the internal REST requests by new class method calls.
  • (re)unifies the local for calculating filter data and reduces the complexity of that process. I use the original get_attribute_counts method instead one updated by Filter data count mismatch #42466 because calculating the attribute count from the results of the sub query is simpler and easier to maintain. I modify the query clauses of the sub query to ensure it match the actual query, resulting the correct attribute filter count.

How to test the changes in this Pull Request:

Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:

Global query

  1. Edit the Product Catalog at Themes > Edit Site > Templates > Product Catalog.
  2. Add the Collection Filters block to the template. See it added with some default filter blocks.
  3. Toggle the Display product count count for all filter blocks except the Price Filter block.
  4. On the frontend, see:
  • The price filter show the correct range.
  • The stock, attribute, rating filter show correct product count.
  • Toggle some filters and reload, see the product count is updated to reflect the filtered products. (The issue that requires reloading the page to see the updated filters will be fixed in separate PRs) This is now fixed and do not require any manual reload.

Repeat the step above with:

  • Archive templates like Product Category, Product Tag, and Product Attribute archive page.
  • Product Search Results.

Local query

Note that the local query support hasn't been finished yet, we need to update the Product Collection block to handle filter parameter by query ID, not globally as it is right now, so multiple product collection blocks with filter aren't possible yet.

  1. Add the Product Collection block to a new page.
  2. Adjust the Product Collection to make it show an unique set of products.
  3. Inside the Product Collection block, add the Collection Filters block as an inner block. See it added with some default filter blocks.
  4. Toggle the Display product count count for all filter blocks except the Price Filter block.
  5. On the frontend, see:
  • The price filter show the correct range.
  • The stock, attribute, rating filter show correct product count.
  • Toggle some filters and reload, see the product count is updated to reflect the filtered products. (The issue that requires reloading the page to see the updated filters will be fixed in separate PRs)

Changelog entry

  • Automatically create a changelog entry from the details below.

Significance

  • Patch
  • Minor
  • Major

Type

  • Fix - Fixes an existing bug
  • Add - Adds functionality
  • Update - Update existing functionality
  • Dev - Development related task
  • Tweak - A minor adjustment to the codebase
  • Performance - Address performance issues
  • Enhancement - Improvement to existing functionality

Message

Comment

Add QueryFilters class to calculate filter data for product queries. Update new interactive filter blocks to use QueryFilters instead of internal REST API requests.

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Dec 14, 2023
@dinhtungdu dinhtungdu changed the title WIP: a dedicate class to get the product count from the query vars WIP: a dedicated class to get the product count from the query vars Dec 14, 2023
Copy link
Contributor

github-actions bot commented Dec 14, 2023

Test Results Summary

Commit SHA: a4ce2e1

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202610m 37s
E2E Tests258003026117m 20s

To view the full API test report, click here.
To view the full E2E test report, click here.
To view all test reports, visit the WooCommerce Test Reports Dashboard.

@dinhtungdu dinhtungdu changed the title WIP: a dedicated class to get the product count from the query vars Add: QueryFilters class to calculate filter data for product queries Dec 18, 2023
@dinhtungdu dinhtungdu self-assigned this Dec 18, 2023
@dinhtungdu dinhtungdu added the block-type: filter blocks Issues related to all of the filter blocks. label Dec 18, 2023
@dinhtungdu dinhtungdu marked this pull request as ready for review December 18, 2023 14:48
Copy link
Contributor

Hi @samueljseay,

Apart from reviewing the code changes, please make sure to review the testing instructions as well.

You can follow this guide to find out what good testing instructions should look like:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

Copy link
Contributor

@samueljseay samueljseay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tested well for me and the code looks good.

@kmanijak
Copy link
Contributor

Things I found during testing:

Global context

Stock Status filter seems to operate with AND operator in some circumstances

When the Product Collection inherits query from template and I choose "In stock" and either "Out of stock" or "On backorder" (that show 0 products), then no results are displayed. Please check the video:

stock-status.mov

It works correctly with the Local context.

Changing filters quickly is not reflected in frontend

If I click couple of filters quickly the URL the products are not synchronised. See the video:

https://github.com/woocommerce/woocommerce/assets/20098064/8e2e373e-58a5-4ef1-aedb-c7bb916d57d8%0A

Local context

I didn't spot any problems with querying data with Local context.

Great job overall! I didn't play with Collection Filters for some time and I'm super impressed! 🚀

@dinhtungdu
Copy link
Member Author

@kmanijak I fixed the issue with stock status, the condition should be OR always.

@dinhtungdu dinhtungdu changed the title Add: QueryFilters class to calculate filter data for product queries [Experimental] Add: QueryFilters class to calculate filter data for product queries Dec 26, 2023
Copy link
Contributor

@samueljseay samueljseay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good. Tested various cases on JN and it works well for me.

@dinhtungdu dinhtungdu merged commit bd9ddc8 into trunk Jan 1, 2024
29 of 30 checks passed
@dinhtungdu dinhtungdu deleted the add/collection-filterer branch January 1, 2024 03:18
@github-actions github-actions bot added this to the 8.6.0 milestone Jan 1, 2024
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label Jan 1, 2024
@nigeljamesstevenson nigeljamesstevenson added status: analysis complete Indicates if a PR has been analysed by Solaris and removed needs: analysis Indicates if the PR requires a PR testing scrub session. labels Jan 2, 2024
opr pushed a commit that referenced this pull request Jan 3, 2024
…roduct queries (#42811)

* add: query filters count class

* chore: method comments

* refactor: rename class to QueryFilters

* fix: price filter count

* fix: price filter url

* chore: remove unnecessary transformation

* chore: remove unused meta query filter

* chore: put public methods on the top of the class

* fix: passing $wp_query

* Add changefile(s) from automation for the following project(s): woocommerce-blocks, woocommerce

* fix: stock query clause

* fix: handle and query type for attribute filter

* fix: ensure passing non null value to floor/ceil

* [Experimental] Collection Filter blocks: ensure the namespace is always rendered (#43112)

* fix: ensure the namespace is always rendered

* fix: target only wc queries

* fix: properly reset part of query to make new filter blocks work with Product Collection block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
block-type: filter blocks Issues related to all of the filter blocks. plugin: woocommerce Issues related to the WooCommerce Core plugin. status: analysis complete Indicates if a PR has been analysed by Solaris
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Product query mismatch between the Product Collection and filter blocks
4 participants