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

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

Closed
jelledruut opened this issue Nov 16, 2023 · 1 comment · Fixed by #42983
Closed

Comments

@jelledruut
Copy link

jelledruut commented Nov 16, 2023

Describe the bug

The /products endpoint accepts the parameter type=variation, which returns product variations, instead of products.

An example request:
/wp-json/wc/store/v1/products?type=variation&parent=24958&attributes[0][attribute]=pa_size&attributes[0][slug]=44

However, when combining type=variation with the attributes parameter, the query becomes incorrect. The example I provided should return all variations that have the product attribute term pa_size 44, with parent product ID 24958.

However, instead it returns no variations when using the attributes parameter in combination with type=variation, since the resulting query is incorrect.

https://github.com/woocommerce/woocommerce-blocks/blob/trunk/src/StoreApi/docs/products.md#product-variations
https://github.com/woocommerce/woocommerce-blocks/blob/trunk/src/StoreApi/docs/products.md#list-products

Expected behavior

When using type=variation and the attributes parameter, it should return all variations that have those attribute terms.

For example, this query would work (for the example URL provided) - since attribute terms linked to variations are stored in the wp_postmeta table for variations:

...
'meta_query' => [
    [
        'key' => 'attribute_pa_size',
        'value' => ['44'],
        'compare' => 'IN',
    ],
],

While it currently results in this query (which only works for products, not variations):

...
'tax_query' => [
    [
        'taxonomy' => 'pa_size',
        'field' => 'slug',
        'terms' => ['44'],
        'operator' => 'IN',
    ],
],

Though an issue that arises is that only the slug is stored for variation attribute terms in wp_postmeta as meta_value, and not the term_id, which is supported for the attributes parameter.

My knowledge of the internals of WooCommerce isn't good enough to figure out a better solution. E.g. maybe the wc_product_attributes_lookup table?

Screenshots

image image image image image

Environment

WordPress:

  • WordPress version: 6.4.1
  • WooCommerce version: 8.2.2
  • Site language: EN
@jelledruut jelledruut added the type: bug The issue is a confirmed bug. label Nov 16, 2023
@roykho
Copy link
Member

roykho commented Nov 20, 2023

Thanks for reporting this. The endpoint documentation mentioned indeed seems to suggest it should be possible. We'll have a look at this.

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

Successfully merging a pull request may close this issue.

3 participants