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

Store API: Add support to query product_variations by attributes #42983

Merged

Conversation

tjcafferkey
Copy link
Contributor

@tjcafferkey tjcafferkey commented Dec 20, 2023

Submission Review Guidelines:

Changes proposed in this Pull Request:

Currently you cannot query product variations by attributes using the Store API, despite the documentation saying that it is possible.

In WooCommerce, when querying product variations specifically, the tax_query is not used directly because product attributes (like size, color, etc.) associated with variations are stored as post meta, not as taxonomy terms. Hence, you need to use meta_query instead of tax_query when dealing with product variations. Product variations are considered as individual products tied to the main product. These variations hold their specific attributes as meta values in the database, rather than being treated as taxonomy terms.

This PR introduces a method which converts the tax_query into a meta_query for types product_variation only.

Known limitation: This will still return no results if you're filtering by attribute ID, in order for this to work you need to pass the attribute slug.

Closes #42093

How to test the changes in this Pull Request:

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

Using the sample products: sample_products.csv

  1. Confirm issue prior without this PR beforehand with the following request (you can use Postman or any other client): /wp-json/wc/store/v1/products?attributes[0][attribute]=pa_color&attributes[0][slug]=red&type=variation This should return no results.
  2. Apply this PR and rerun the request.
  3. Confirm results returned as as expected.
  4. Try querying products instead of product variations (by removing type=variation) to confirm this still works as expected and no regression has been introduced.

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

Store API: Add support to query product_variations by attribute slugs.

Comment

@tjcafferkey tjcafferkey added type: bug The issue is a confirmed bug. plugin: woocommerce Issues related to the WooCommerce Core plugin. team: Kirigami & Origami labels Dec 20, 2023
@tjcafferkey tjcafferkey self-assigned this Dec 20, 2023
@tjcafferkey tjcafferkey marked this pull request as ready for review December 20, 2023 09:17
Copy link
Contributor

github-actions bot commented Dec 20, 2023

Test Results Summary

Commit SHA: 17204ea

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

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.

@wavvves wavvves self-requested a review December 20, 2023 13:05
Copy link
Contributor

Hi @wavvves,

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

@wavvves wavvves left a comment

Choose a reason for hiding this comment

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

👍🏼

@tjcafferkey tjcafferkey merged commit a9550d8 into trunk Dec 21, 2023
20 checks passed
@tjcafferkey tjcafferkey deleted the update/store-api-product-variation-attributes-support branch December 21, 2023 08:09
@github-actions github-actions bot added this to the 8.6.0 milestone Dec 21, 2023
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label Dec 21, 2023
@veljkho veljkho added needs: internal testing Indicates if the PR requires further testing conducted by Solaris 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 Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: internal testing Indicates if the PR requires further testing conducted by Solaris plugin: woocommerce Issues related to the WooCommerce Core plugin. status: analysis complete Indicates if a PR has been analysed by Solaris team: Kirigami & Origami type: bug The issue is a confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Combining type=variation with attributes for Store API /products endpoint doesn't work
3 participants