Skip to content

Conversation

@jorgeatorres
Copy link
Member

@jorgeatorres jorgeatorres commented Dec 28, 2022

All Submissions:

Changes proposed in this Pull Request:

Since 7.1, the version of woocommerce-blocks bundled with WC, is registering query vars associated to product filters (say filter_color or filter_size) as proper WP query vars for their filters.

When a product filter is in use, those $_GET params become query vars in the main query, which isn't a problem most of the time, but it is when the "shop" page is also the home page. That's because WP assumes the home page can only have certain query vars active. Otherwise, the request is not considered a 'page' request and it's instead marked with is_home = true (which is incorrect in this case). This in turn breaks our code in WC_Query which handles this scenario. The result is that the filters don't work and you get a weird posts page instead of products.

This PR permits WC_Query to "correct" the main query (by correctly setting is_home, is_page, page_id, etc.) when product filter query vars are in use.

Closes #35983.

For discussion

The PR assumes we only want a few variables to be considered 'valid' variables on the front page, following what WP does. That said, this approach isn't flexible, as exemplified by breakage resulting from WC Blocks adding a few query vars. An alternative would be to just allow any query vars on this page (which would be even easier at the code level), but then this could possibly break other code too.

  • This PR is a very minor change/addition and does not require testing instructions (if checked you can ignore/remove the next section).

How to test the changes in this Pull Request:

  1. Go to Settings > Reading. Select the radio button labeled "A static page" and choose your "Shop" page from in the "Homepage" dropdown.
  2. Go to Appearance > Widgets and add some product filter widgets, such as "Filter by attribute" or "Filter by price".
  3. Go to your site's homepage.
  4. You should see the regular "Shop" page with products.
  5. Use any of the filter widgets to filter the results.
  6. Confirm that:
    • On trunk, filtering doesn't work.
    • On this branch, filtering works.

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Dec 28, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Dec 28, 2022

Test Results Summary

Commit SHA: a5a2743

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202610m 55s
E2E Tests189006019514m 47s

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.

@jorgeatorres
Copy link
Member Author

Something I wanted to add is that due to this weird WP behavior, any query var might break things on the front page, so it's not exclusive of product filters.

For example, activating the following snippet on a site breaks the front page if you pass ?mysuperarg=1 in the query string.

add_filter( 'query_vars', function( $vars ) {
	$vars[] = 'mysuperarg';
	return $vars;
} );

@jorgeatorres
Copy link
Member Author

Hey @vedanshujain!

Per our discussion, could we merge this soon-ish? There have been other reports of this. Even if this fix isn't 100% future-proof, it should cover what the filter blocks support now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

plugin: woocommerce Issues related to the WooCommerce Core plugin.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Product Filter Widgets not working when Shop is on Homepage

3 participants