-
Notifications
You must be signed in to change notification settings - Fork 219
Enhance: Bring back user to 1st page when filters are changed #9952
Enhance: Bring back user to 1st page when filters are changed #9952
Conversation
This commit updates the `ProductCollection` block implementation to add support for filter blocks, including the Price filter, Attributes filter, Rating filter, and In stock filter. The changes include: 1. Adding a new property `$attributes_filter_query_args` to store the query arguments related to the filter by attributes block. 2. Adding a new method `add_support_for_filter_blocks()` to enable support for filter blocks. This method sets the necessary asset data to enable filtering and refreshes the page when a filter is applied. 3. Adding new methods to handle specific filter queries, including `get_filter_by_price_query()`, `get_filter_by_attributes_query()`, `get_filter_by_stock_status_query()`, and `get_filter_by_rating_query()`. These methods generate the respective queries based on the applied filters. 4. Refactoring the `get_final_query_args()` method to include the newly added filter queries using the `get_queries_by_applied_filters()` method. These changes enhance the functionality of the `ProductCollection` block by allowing users to filter products based on price, attributes, rating, and stock status.
This commit enhances the ProductCollection block to support various filter blocks such as Price filter block, Attributes filter block, Rating filter block, and In stock filter block. The `build_query` method has been refactored into two separate methods: `build_frontend_query` and `get_final_frontend_query` to make the code more modular and readable. The `add_support_for_filter_blocks` method has been modified to support the generation of product IDs for filter blocks. The method `update_rest_query` has been renamed to `update_rest_query_in_editor` for better clarity and understanding of its function. Similarly, `get_final_query_args` has been refactored to include the `$is_exclude_applied_filters` parameter which helps in generating product IDs for the filter blocks. Moreover, the filter hook `pre_render_block` has been added to support the filtering of blocks before they are rendered. This update will enhance the user experience by providing more filtering options in the ProductCollection block.
…l-with-filter-blocks
…l-with-filter-blocks
…l-with-filter-blocks
This commit refactors the changeUrl function in the `filters.ts` file. The purpose of this refactor is to remove the page number from the URL whenever filters are changed. This ensures that the user is always taken to the first page of results. The commit includes the following changes: - Added comments explaining the different page number formats in the URL. - Updated the `newUrl` parameter by removing the page number using regular expressions. - The page is then reloaded with the updated URL using `window.location.href`. These changes improve the URL handling in the `changeUrl` function and enhance the user experience when interacting with filters.
The release ZIP for this PR is accessible via:
Script Dependencies ReportThere is no changed script dependency between this branch and trunk. This comment was automatically generated by the TypeScript Errors Report
🎉 🎉 This PR does not introduce new TS errors. |
Size Change: +877 B (0%) Total Size: 1.17 MB
ℹ️ View Unchanged
|
…l-with-filter-blocks-2
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.
LGTM!
…l-with-filter-blocks-2
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.
I noticed that when removing all filters, the URL becomes /?
first and then /
, causing an unnecessary reload:
Enregistrament.de.pantalla.des.de.2023-06-22.14-56-46.webm
Can you reproduce as well?
@Aljullu, I can reproduce the issue. Thank you for identifying this edge case. I will work on fixing it and push the necessary changes 🤝 |
This commit optimizes the `changeUrl` utility function to prevent unnecessary page reloads. Changes: 1. It checks if there are no query parameters in the URL and the URL ends with a '?'. 2. If these conditions are met, it removes the trailing '?' from the URL. The reason for this change is that a '?' in the URL typically indicates that query parameters will follow. If there are no query parameters, the '?' is unnecessary and can cause the page to reload, which can negatively affect performance. Therefore, by removing the '?', we prevent this unnecessary page reload.
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.
This is working great, @imanish003! I just left one small comment about a possible small optimization, but besides that, LGTM.
assets/js/utils/filters.ts
Outdated
const numberOfQueryParams = [ ...urlObject.searchParams.keys() ].length; | ||
if ( numberOfQueryParams === 0 && newUrl.endsWith( '?' ) ) { |
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.
I think it's safe to skip the query params check (numberOfQueryParams === 0
), no? I mean, if the URL ends with ?
, it's always safe to remove the last question mark, no matter if there are query params or not, no? 🤔
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.
I think so. I was just trying to be extra careful to ensure I do not break any existing functionality.
But good point, the trailing "?" can indeed be removed safely without checking for query parameters. I will simplify the code accordingly. Thanks for the suggestion! 🙌🏻
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.
…l-with-filter-blocks-2
This commit simplifies the removal of trailing '?' characters from URLs. The previous implementation checked if there were any query parameters before removing a trailing '?'. However, upon review, it was determined that this check was unnecessary, as a trailing '?' could be removed safely even if query parameters were present, without any negative impact. This change simplifies the code and prevents unnecessary computation checking for the existence of query parameters.
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.
Thanks for the update, @imanish003. I just found one last minor issue: in some occassions I end up with two &&
in the URL.
Steps to reproduce:
- Add the Filter by Price and Filter by Attribute blocks alongside the Product Collection block.
- In the frontend, add a price filter so pagination is still available. The URL will look something like
/?min_price=15
. - Using pagination, switch to page two. The URL will look something like
/?query-0-page=2&min_price=15
. - Now, add an attribute filter. The URL will look something like
/?min_price=15&&filter_color=blue&query_type_color=or
. Notice the double&&
beforefilter_color
.
Can you reproduce as well?
Hi @Aljullu, This issue isn't reproducing for me 🤔 Here is a quick demo of what I tried: Screen.Recording.2023-07-05.at.12.18.37.PM.movIf you are still able to reproduce the issue then maybe I can add a logic like this to clean up query params:
What do you think? 🙂 |
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.
Ups, I don't seem to be able to reproduce anymore, sorry for the confusion. 😕 Not sure what was wrong with my setup, I will open an issue if I can reproduce again, but for now, let's merge this PR. 🚀
URL page number removal on filter change: When filters are changed, the page number in the URL is removed. This is to ensure that when a filter is applied, the user is taken back to the first page of results. The code accommodates three different formats of page numbers in the URL and removes them accordingly.
Part of #9876
Testing
WooCommerce Visibility
Changelog