Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Fix Product categories, Product Tags & Keyword filter not working in Products block #8377

Merged
merged 12 commits into from
Feb 21, 2023

Conversation

imanish003
Copy link
Contributor

@imanish003 imanish003 commented Feb 3, 2023

This PR fix this issue, according to which following filters set from Inspector Controls for Products block aren't working:

  • Product Categories
  • Product tags
  • Keyword

Fixes #8249

Testing

  1. Create a new post
  2. Add Products block inside Editor
  3. Add Product categories and/or Product Tags and/or Keyword to Filters(see screenshot)
    image
  4. Check the front-end. Only products with added categories, tags, or keywords are showing.
  • Do not include in the Testing Notes

WooCommerce Visibility

  • WooCommerce Core
  • Feature plugin
  • Experimental

Changelog

Fix Product categories, Product Tags & Keyword filter not working in Products block

@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2023

The release ZIP for this PR is accessible via:

https://wcblocks.wpcomstaging.com/wp-content/uploads/woocommerce-gutenberg-products-block-8377.zip

Script Dependencies Report

The compare-assets action has detected some changed script dependencies between this branch and trunk. Please review and confirm the following are correct before merging.

Script Handle Added Removed
reviews-frontend.js react, wc-settings, wp-a11y, wp-api-fetch, wp-compose, wp-element, wp-i18n, wp-is-shallow-equal, wp-polyfill ⚠️
active-filters-frontend.js lodash, react, wc-blocks-data-store, wc-price-format, wc-settings, wp-data, wp-element, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-polyfill, wp-primitives, wp-url ⚠️
all-products-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-blocks-registry, wc-blocks-shared-context, wc-blocks-shared-hocs, wc-price-format, wc-settings, wp-a11y, wp-api-fetch, wp-autop, wp-block-editor, wp-blocks, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-hooks, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-polyfill, wp-primitives, wp-url, wp-warning, wp-wordcount ⚠️
attribute-filter-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-settings, wp-a11y, wp-block-editor, wp-blocks, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-polyfill, wp-primitives, wp-url, wp-warning ⚠️
cart-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-blocks-registry, wc-blocks-shared-context, wc-blocks-shared-hocs, wc-price-format, wc-settings, wp-a11y, wp-api-fetch, wp-autop, wp-block-editor, wp-blocks, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-hooks, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-plugins, wp-polyfill, wp-primitives, wp-url, wp-warning, wp-wordcount ⚠️
checkout-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-blocks-registry, wc-blocks-shared-hocs, wc-price-format, wc-settings, wp-a11y, wp-api-fetch, wp-autop, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-hooks, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-plugins, wp-polyfill, wp-primitives, wp-url, wp-warning, wp-wordcount ⚠️
filter-wrapper-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-blocks-registry, wc-price-format, wc-settings, wp-a11y, wp-block-editor, wp-blocks, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-polyfill, wp-primitives, wp-url, wp-warning ⚠️
mini-cart-frontend.js wc-settings, wp-polyfill ⚠️
price-filter-frontend.js lodash, react, wc-blocks-data-store, wc-price-format, wc-settings, wp-data, wp-element, wp-i18n, wp-is-shallow-equal, wp-polyfill, wp-url ⚠️
rating-filter-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-settings, wp-a11y, wp-block-editor, wp-blocks, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-polyfill, wp-primitives, wp-url, wp-warning ⚠️
single-product-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-blocks-registry, wc-blocks-shared-context, wc-blocks-shared-hocs, wc-price-format, wc-settings, wp-api-fetch, wp-autop, wp-block-editor, wp-blocks, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-hooks, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-polyfill, wp-primitives, wp-url, wp-warning, wp-wordcount ⚠️
stock-filter-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-settings, wp-a11y, wp-block-editor, wp-blocks, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-polyfill, wp-primitives, wp-url, wp-warning ⚠️
mini-cart-component-frontend.js lodash, react, wc-blocks-checkout, wc-blocks-data-store, wc-blocks-registry, wc-price-format, wc-settings, wp-a11y, wp-autop, wp-compose, wp-data, wp-deprecated, wp-dom, wp-element, wp-hooks, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-polyfill, wp-primitives, wp-url, wp-warning, wp-wordcount ⚠️

This comment was automatically generated by the ./github/compare-assets action.

TypeScript Errors Report

  • Files with errors: 493
  • Total errors: 2341

🎉 🎉 This PR does not introduce new TS errors.

comments-aggregator

@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2023

Size Change: 0 B

Total Size: 1.1 MB

ℹ️ View Unchanged
Filename Size
build/active-filters-frontend.js 7.98 kB
build/active-filters-wrapper-frontend.js 5.99 kB
build/active-filters.js 7.32 kB
build/add-to-cart-form.js 1.48 kB
build/all-products-frontend.js 11.7 kB
build/all-products.js 34.6 kB
build/all-reviews.js 7.66 kB
build/attribute-filter-frontend.js 22.4 kB
build/attribute-filter-wrapper-frontend.js 7.09 kB
build/attribute-filter.js 12.4 kB
build/blocks-checkout.js 41.2 kB
build/breadcrumbs.js 2.04 kB
build/cart-blocks/cart-accepted-payment-methods-frontend.js 1.38 kB
build/cart-blocks/cart-cross-sells-frontend.js 253 B
build/cart-blocks/cart-cross-sells-products-frontend.js 9.67 kB
build/cart-blocks/cart-express-payment--checkout-blocks/express-payment-frontend.js 5.19 kB
build/cart-blocks/cart-express-payment-frontend.js 719 B
build/cart-blocks/cart-items-frontend.js 299 B
build/cart-blocks/cart-line-items--mini-cart-contents-block/products-table-frontend.js 5.36 kB
build/cart-blocks/cart-line-items-frontend.js 1.06 kB
build/cart-blocks/cart-order-summary-frontend.js 1.24 kB
build/cart-blocks/cart-totals-frontend.js 321 B
build/cart-blocks/empty-cart-frontend.js 345 B
build/cart-blocks/filled-cart-frontend.js 655 B
build/cart-blocks/order-summary-coupon-form-frontend.js 1.62 kB
build/cart-blocks/order-summary-discount-frontend.js 2.12 kB
build/cart-blocks/order-summary-fee-frontend.js 274 B
build/cart-blocks/order-summary-heading-frontend.js 455 B
build/cart-blocks/order-summary-shipping-frontend.js 14.8 kB
build/cart-blocks/order-summary-subtotal-frontend.js 274 B
build/cart-blocks/order-summary-taxes-frontend.js 435 B
build/cart-blocks/proceed-to-checkout-frontend.js 1.24 kB
build/cart-frontend.js 28.9 kB
build/cart.js 47.3 kB
build/catalog-sorting.js 1.7 kB
build/checkout-blocks/actions-frontend.js 1.84 kB
build/checkout-blocks/billing-address--checkout-blocks/shipping-address-frontend.js 3.91 kB
build/checkout-blocks/billing-address-frontend.js 1.18 kB
build/checkout-blocks/contact-information-frontend.js 2.05 kB
build/checkout-blocks/express-payment-frontend.js 1.13 kB
build/checkout-blocks/fields-frontend.js 345 B
build/checkout-blocks/order-note-frontend.js 1.14 kB
build/checkout-blocks/order-summary-cart-items-frontend.js 3.67 kB
build/checkout-blocks/order-summary-coupon-form-frontend.js 1.78 kB
build/checkout-blocks/order-summary-discount-frontend.js 2.29 kB
build/checkout-blocks/order-summary-fee-frontend.js 277 B
build/checkout-blocks/order-summary-frontend.js 1.24 kB
build/checkout-blocks/order-summary-shipping-frontend.js 14.9 kB
build/checkout-blocks/order-summary-subtotal-frontend.js 275 B
build/checkout-blocks/order-summary-taxes-frontend.js 435 B
build/checkout-blocks/payment-frontend.js 8.45 kB
build/checkout-blocks/pickup-options-frontend.js 2.8 kB
build/checkout-blocks/shipping-address-frontend.js 1.14 kB
build/checkout-blocks/shipping-method-frontend.js 2.27 kB
build/checkout-blocks/shipping-methods-frontend.js 4.78 kB
build/checkout-blocks/terms-frontend.js 1.56 kB
build/checkout-blocks/totals-frontend.js 324 B
build/checkout-frontend.js 30.4 kB
build/checkout.js 43.9 kB
build/customer-account.js 3.12 kB
build/featured-category.js 13.3 kB
build/featured-product.js 13.6 kB
build/filter-wrapper-frontend.js 14.1 kB
build/filter-wrapper.js 2.39 kB
build/general-style-rtl.css 1.31 kB
build/general-style.css 1.31 kB
build/handpicked-products.js 7.24 kB
build/legacy-template.js 2.85 kB
build/mini-cart-component-frontend.js 28 kB
build/mini-cart-contents-block/empty-cart-frontend.js 366 B
build/mini-cart-contents-block/filled-cart-frontend.js 268 B
build/mini-cart-contents-block/footer-frontend.js 2.86 kB
build/mini-cart-contents-block/items-frontend.js 237 B
build/mini-cart-contents-block/products-table-frontend.js 590 B
build/mini-cart-contents-block/shopping-button-frontend.js 313 B
build/mini-cart-contents-block/title-frontend.js 367 B
build/mini-cart-contents.js 17.1 kB
build/mini-cart-frontend.js 2 kB
build/mini-cart.js 4.3 kB
build/price-filter-frontend.js 13.8 kB
build/price-filter-wrapper-frontend.js 6.99 kB
build/price-filter.js 8.38 kB
build/price-format.js 1.19 kB
build/product-add-to-cart--product-button--product-category-list--product-image--product-price--product-r--a0326d00.js 253 B
build/product-add-to-cart--product-button--product-image--product-rating--product-title.js 151 B
build/product-add-to-cart-frontend.js 6.7 kB
build/product-add-to-cart.js 8.61 kB
build/product-best-sellers.js 7.6 kB
build/product-button--product-category-list--product-image--product-price--product-rating--product-sale-b--e17c7c01.js 495 B
build/product-button--product-image--product-price--product-rating--product-sale-badge--product-title.js 259 B
build/product-button-frontend.js 2.19 kB
build/product-button.js 3.99 kB
build/product-categories.js 2.36 kB
build/product-category-list-frontend.js 1.19 kB
build/product-category-list.js 503 B
build/product-category.js 8.58 kB
build/product-image-frontend.js 2.2 kB
build/product-image.js 4.09 kB
build/product-new.js 7.58 kB
build/product-on-sale.js 7.91 kB
build/product-price-frontend.js 2.29 kB
build/product-price.js 1.58 kB
build/product-query.js 6.08 kB
build/product-rating-frontend.js 1.62 kB
build/product-rating.js 918 B
build/product-results-count.js 1.65 kB
build/product-sale-badge-frontend.js 1.43 kB
build/product-sale-badge.js 818 B
build/product-search.js 2.63 kB
build/product-sku-frontend.js 629 B
build/product-sku.js 378 B
build/product-stock-indicator-frontend.js 1.32 kB
build/product-stock-indicator.js 645 B
build/product-summary-frontend.js 1.58 kB
build/product-summary.js 919 B
build/product-tag-list-frontend.js 1.18 kB
build/product-tag-list.js 496 B
build/product-tag.js 8.06 kB
build/product-title-frontend.js 1.62 kB
build/product-title.js 3.46 kB
build/product-top-rated.js 7.83 kB
build/products-by-attribute.js 8.53 kB
build/rating-filter-frontend.js 21.4 kB
build/rating-filter-wrapper-frontend.js 6.19 kB
build/rating-filter.js 7.39 kB
build/reviews-by-category.js 11.2 kB
build/reviews-by-product.js 12.3 kB
build/reviews-frontend.js 7.15 kB
build/single-product-frontend.js 17.8 kB
build/single-product.js 9.97 kB
build/stock-filter-frontend.js 21.1 kB
build/stock-filter-wrapper-frontend.js 5.85 kB
build/stock-filter.js 8.12 kB
build/store-notices.js 1.65 kB
build/vendors--attribute-filter-wrapper--cart-blocks/cart-cross-sells-products--cart-blocks/order-summary--82e4ed06-frontend.js 6.86 kB
build/vendors--attribute-filter-wrapper--rating-filter-wrapper--stock-filter-wrapper-frontend.js 7.69 kB
build/vendors--cart-blocks/cart-cross-sells-products--cart-blocks/cart-line-items--cart-blocks/cart-order--3c5fe802-frontend.js 5.26 kB
build/vendors--cart-blocks/cart-cross-sells-products--cart-blocks/order-summary-shipping--checkout-blocks--18f9376a-frontend.js 19.4 kB
build/vendors--cart-blocks/cart-cross-sells-products--product-add-to-cart-frontend.js 7.25 kB
build/vendors--cart-blocks/cart-line-items--checkout-blocks/order-summary-cart-items--mini-cart-contents---233ab542-frontend.js 3.14 kB
build/vendors--cart-blocks/order-summary-shipping--checkout-blocks/billing-address--checkout-blocks/order--5b8feb0b-frontend.js 4.83 kB
build/vendors--checkout-blocks/shipping-method-frontend.js 12 kB
build/vendors--checkout-blocks/shipping-methods-frontend.js 9.48 kB
build/wc-blocks-data.js 21.4 kB
build/wc-blocks-editor-style-rtl.css 5.68 kB
build/wc-blocks-editor-style.css 5.68 kB
build/wc-blocks-google-analytics.js 1.56 kB
build/wc-blocks-middleware.js 933 B
build/wc-blocks-registry.js 3.15 kB
build/wc-blocks-shared-context.js 1.52 kB
build/wc-blocks-shared-hocs.js 1.73 kB
build/wc-blocks-style-rtl.css 26.7 kB
build/wc-blocks-style.css 26.7 kB
build/wc-blocks-vendors-style-rtl.css 1.96 kB
build/wc-blocks-vendors-style.css 1.96 kB
build/wc-blocks-vendors.js 64.4 kB
build/wc-blocks.js 2.65 kB
build/wc-payment-method-bacs.js 816 B
build/wc-payment-method-cheque.js 811 B
build/wc-payment-method-cod.js 909 B
build/wc-payment-method-paypal.js 837 B
build/wc-settings.js 2.6 kB
build/wc-shipping-method-pickup-location.js 29.7 kB
build/woo-directives-runtime.js 2.52 kB
build/woo-directives-vendors.js 7.91 kB

compressed-size-action

@imanish003 imanish003 added block-type: product-query Issues related to/affecting all product-query variations. type: bug The issue/PR concerns a confirmed bug. focus: FSE Work related to prepare WooCommerce for FSE. labels Feb 3, 2023
Comment on lines 489 to 492
// "Product Categories" & "Product Tags" could be provided using "Filters" ToolsPanel available in Inspector Controls.
if ( isset( $query['tax_query'] ) ) {
$result['tax_query'] = $this->get_filter_by_product_categories_or_tags_query( $query['tax_query'] );
}
Copy link
Contributor Author

@imanish003 imanish003 Feb 3, 2023

Choose a reason for hiding this comment

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

@sunyatasattva @gigitux Can you please take a look at this PR? I have a query regarding these lines of code.

As I am not sure what else could be there in $query['tax_query'], therefore I have created get_filter_by_product_categories_or_tags_query function to extract queries related to only Product categories & Product tags.

I am wondering if we could use $query['tax_query'] as it is 🤔 Is there any possibility of extra items in $query['tax_query'] which we won't need & therefore we need to extract only what we need((For example, only Product categories & tags))?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, I can see that there is get_global_query function. But code of this function only runs if is_custom_inherit_global_query_implementation_enabled is true.

Only this function extracts Keyword which could be provided using "Filters" ToolsPanel available in Inspector Controls. Don't we need Keyword when is_custom_inherit_global_query_implementation_enabled is false?

Copy link
Contributor

Choose a reason for hiding this comment

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

I am wondering if we could use $query['tax_query'] as it is 🤔 Is there any possibility of extra items in $query['tax_query'] which we won't need & therefore we need to extract only what we need((For example, only Product categories & tags))?

I don't recall exactly the reasoning behind it, as @gigitux has worked on that part of the code, but I believe it is intentional. Perhaps something to do with the interaction with filters blocks if we take tax_query as it is?

Or maybe it was a prophylactic measure to enable the custom_inherit_global_query, which we eventually want to switch to.

Don't we need Keyword when is_custom_inherit_global_query_implementation_enabled is false?

Yes, we definitely do. It must have been an oversight we didn't catch, as we were focused on implementing the custom inherit, before the scope was changed.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't remember very well, tbh 😭.

In any case, I think that we should not use the $query['tax_query'] as it is to be ready when we will switch to the custom inherit query logic.

We already have the logic to merge the queries.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @gigitux 👋

In any case, I think that we should not use the $query['tax_query'] as it is to be ready when we will switch to the custom inherit query logic.

Got it, then the changes I made in this PR looks good to me. In short, I created get_filter_by_product_categories_or_tags_query function to extract product_tag & product_cat from the tax_query as you can see here. Also, I extracted the keyword here. Please let me know if you notice any problem with this implementation.

We already have the logic to merge the queries.

Right, but while calling merge_queries function, we were not passing tax_query for product categories, product tags & keyword. I made the changes in this PR to pass these to merge_queries function.

Please let me know if I misunderstood something & if you see any problem with changes I made in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gigitux That makes sense. There are two sections of filters in Editor i.e. Advance Filters & Filters (see screenshot)
image

As per my understanding, we are creating queries for Advance filters in get_queries_by_attributes (which isn't obvious from the name of function).

I am thinking to do one of the following:

  1. Maybe we can rename this function to get_filter_queries & return queries for Advance Filters & Filters from this function.
  2. Rename this function to get_advance_filter_queries to make it more readable and clear. And, create new function called get_filter_queries which will return queries for product categories, product tags & keyword i.e. all the filters. Then pass get_filter_queries() as argument here.

Which of these you think would be better approach?

Copy link
Contributor

Choose a reason for hiding this comment

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

As per my understanding, we are creating queries for Advance filters in get_queries_by_attributes (which isn't obvious from the name of function).

That function generates queries by the custom attributes that the merchant sets via the editor. Furthermore, I think that there should not be a relationship between the UI and the name of the function of the backend.

So, I would create another function to create a valid query object starting from the $tax_query object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That function generates queries by the custom attributes that the merchant sets via the editor.

Got it, maybe we can rename the function to get_queries_by_custom_attributes just to make it more readable & clear. What do you think? 🤔

That function generates queries by the custom attributes that the merchant sets via the editor. Furthermore, I think that there should not be a relationship between the UI and the name of the function of the backend.
So, I would create another function to create a valid query object starting from the $tax_query object.

That makes sense. I will create a new function. Thanks for the feedback 🙌🏻

Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think?

It makes sense! 👍

That makes sense. I will create a new function. Thanks for the feedback 🙌🏻

💪

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gigitux I made the changes in this commit. Would you like to have a quick look?

Copy link
Contributor

@sunyatasattva sunyatasattva left a comment

Choose a reason for hiding this comment

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

I'm getting some strange behaviour.

Sometimes when I input a category, I get this console error, and autocomplete doesn't work. Since the category input is a core one, it might be a bug from there, but worth mentioning:

Screenshot 2023-02-03 at 18 56 42

Otherwise, when I input a broad category such as “Clothing”, in the front-end I only get the results of that category and not its subcategories. E.g. “Clothing > Hoodies” do not appear. I believe the expected result of when I choose a broad category, would be to show all products from that category, including children. What do you think?

Keywords and tags seem to work correctly.

@github-actions
Copy link
Contributor

This PR has been marked as stale because it has not seen any activity within the past 7 days. Our team uses this tool to help surface pull requests that have slipped through review.

If deemed still relevant, the pr can be kept active by ensuring it's up to date with the main branch and removing the stale label.

@github-actions github-actions bot added status: stale Stale issues and PRs have had no updates for 60 days. and removed status: stale Stale issues and PRs have had no updates for 60 days. labels Feb 11, 2023
@imanish003
Copy link
Contributor Author

imanish003 commented Feb 14, 2023

Hi @sunyatasattva, Thanks for the feedback 🙌🏻

Sometimes when I input a category, I get this console error, and autocomplete doesn't work. Since the category input is a core one, it might be a bug from there, but worth mentioning:

I tried to reproduce it by adding & removing the categories, but I wasn't able to reproduce it. Can you please share the steps to reproduce it? IMO We can report the issue on GB repo once we know how to reproduce this issue.

when I input a broad category such as “Clothing”, in the front-end I only get the results of that category and not its subcategories. E.g. “Clothing > Hoodies” do not appear. I believe the expected result of when I choose a broad category, would be to show all products from that category, including children. What do you think?

Looks like this issue is there in Query Loop block. When a category is selected, it makes sense to show all the sub-category products. IMO we should report this issue on GB repo because when post type is either product or post, categories can be selected & sub-categories product/post doesn't show on frontend. What do you think?

Although, we can also fix it by fetching the sub-categories items. But, IMO it should be fixed in Query Loop block instead.

…he query.

Functions:
- get_filter_by_product_categories_or_tags_query
- get_filter_by_keyword_query

Also rename the function `get_queries_by_attributes` to `get_queries_by_custom_attributes` to make it more clear what it does.
@imanish003 imanish003 marked this pull request as ready for review February 14, 2023 12:06
@woocommercebot woocommercebot requested a review from a team February 14, 2023 12:06
@imanish003 imanish003 changed the title Fix product categories & keyword filter not working Fix Product categories, Product Tags & Keyword filter not working in Products block Feb 14, 2023
@gigitux
Copy link
Contributor

gigitux commented Feb 16, 2023

He, @imanish003! I noticed that this fix doesn't work with custom taxonomies. This how to reproduce the issue:

  • Install Custom Post Type UI plugin
  • Create a new taxonomy (when you create it, you must attach it to the product type).
  • Add a new term to the taxonomy.
  • Create a new post with the PQ.
  • In the section filters, you can find a new input related to the new taxonomy that you added before. Now, filter by the term that you created earlier. You can see that the editor preview and frontend don't work.

A video that shows the issue. I should see only one product, the cap.

Screen.Capture.on.2023-02-16.at.11-11-37.mov

As user can add custom taxonomies, we need to make sure that we include all taxonomies in the query.

To fetch all product taxonomies, I used following function:
```php
get_taxonomies( array( 'object_type' => array( 'product' ) ), 'names' )
```
@imanish003
Copy link
Contributor Author

Hey @gigitux, That's an interesting corner case. Thanks for catching it 🙌🏻

I was using a static array for taxonomies:

$product_taxonomies = [ 'product_cat', 'product_tag' ];

In e6bd862 commit, I have used get_taxonomies function to get all product taxonomies, which fix the issue.

$product_taxonomies = get_taxonomies( array( 'object_type' => array( 'product' ) ), 'names' );

Can you please have a look again?

Copy link
Contributor

@gigitux gigitux left a comment

Choose a reason for hiding this comment

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

Thanks for fixing the issue! 💪 I added some comments, after addressing them I think that we can merge the PR!

@@ -220,8 +222,8 @@ function( $acc, $query ) {
* duplicated items.
*/
if (
! empty( $merged_query['post__in'] ) &&
count( $merged_query['post__in'] ) > count( array_unique( $merged_query['post__in'] ) )
! empty( $merged_query['post__in'] ) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure, but I guess that we should revert this formatting change.

@@ -337,8 +339,8 @@ private function get_stock_status_query( $stock_statii ) {
* meta query for stock status.
*/
if (
count( $stock_statii ) === count( $stock_status_options ) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure, but I guess that we should revert this formatting change.

Comment on lines 624 to 629
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => $filtered_stock_status_values,
'operator' => 'IN',
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure, but I guess that we should revert this formatting change.

*
* @param WP_Query $query The query to extract the keyword filter from.
* @return array The keyword filter, or an empty array if none is found.
* @throws InvalidArgumentException If $query is not an array.
Copy link
Contributor

Choose a reason for hiding this comment

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

I thank that we should do this check, in this way, we will not throw any errors. Now we don't catch the error.

$this->get_queries_by_applied_filters()
$this->get_queries_by_custom_attributes( $parsed_block ),
$this->get_queries_by_applied_filters(),
$this->get_filter_by_taxonomies_query( $query['tax_query'] ),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
$this->get_filter_by_taxonomies_query( $query['tax_query'] ),
$this->get_filter_by_taxonomies_query( isset($query['tax_query'] ? $query['tax_query']: array()),

Now, if the Products block doesn't have any filter defined, tax_query doesn't exist, and WP returns an error.

@imanish003
Copy link
Contributor Author

Hey @gigitux, I truly appreciate your attention to detail 🚀 I have made the necessary revisions based on your recommendations. Can you please have a look again?

Copy link
Contributor

@gigitux gigitux left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for addressing the feedback! :shipit:

Also, I suggest to write E2E tests that check those use-cases. It could make sense do it in a follow-up PR 👍

@github-actions github-actions bot added this to the 9.7.0 milestone Feb 20, 2023
@imanish003
Copy link
Contributor Author

imanish003 commented Feb 21, 2023

@gigitux That makes sense. I will create a follow-up PR. I have created this task for E2E tests. 🙌🏻

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block-type: product-query Issues related to/affecting all product-query variations. focus: FSE Work related to prepare WooCommerce for FSE. type: bug The issue/PR concerns a confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Products Block: Filtering by Category or Tag doesn't Work
3 participants