woocommerce / woocommerce

A customizable, open-source ecommerce platform built on WordPress. Build any commerce solution you can imagine.
https://woocommerce.com
9.21k stars 10.74k forks source link

[Enhancement]: Extend the public Store API #48494

Open 355am opened 3 weeks ago

355am commented 3 weeks ago

Describe the solution you'd like

The WooCommerce public (= no API key needed) Store API enables us to query products via the wp-json/wc/store/products endpoint. It is also possible to filter the results simply by adding the product category ID or product tag ID as a query param for eg. like this: wp-json/wc/store/products?category=1377 .

But there is no way to

  1. filter products based on product category slug (instead of the ID);
  2. get a list of available product categories and/ or tags through the public Store API endpoint (so I am able to figure out the product/ tag ID and then query products based on that).

Since the goal (according to this post) of the WooCommerce Store API is to provide public REST API endpoints for the development of customer-facing cart, checkout, and product functionality, would be nice to include both.

Describe alternatives you've considered

No response

Additional context

No response

mikejolley commented 2 weeks ago

I don't think we necessarily need to duplicate functionality available in other WP APIs. Categories for example can be looked up via /wp-json/wp/v2/product_cat. From that you can derive IDs from slugs—perhaps you can utilise this in your use case?

Example response ```json [ { "id": 19, "count": 5, "description": "", "link": "https://store.local/product-category/clothing/accessories/", "name": "Accessories", "slug": "accessories", "taxonomy": "product_cat", "parent": 16, "meta": [], "_links": { "self": [ { "href": "https://store.local/wp-json/wp/v2/product_cat/19" } ], "collection": [ { "href": "https://store.local/wp-json/wp/v2/product_cat" } ], "about": [ { "href": "https://store.local/wp-json/wp/v2/taxonomies/product_cat" } ], "up": [ { "embeddable": true, "href": "https://store.local/wp-json/wp/v2/product_cat/16" } ], "wp:post_type": [ { "href": "https://store.local/wp-json/wp/v2/product?product_cat=19" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } }, { "id": 16, "count": 14, "description": "", "link": "https://store.local/product-category/clothing/", "name": "Clothing", "slug": "clothing", "taxonomy": "product_cat", "parent": 0, "meta": [], "_links": { "self": [ { "href": "https://store.local/wp-json/wp/v2/product_cat/16" } ], "collection": [ { "href": "https://store.local/wp-json/wp/v2/product_cat" } ], "about": [ { "href": "https://store.local/wp-json/wp/v2/taxonomies/product_cat" } ], "wp:post_type": [ { "href": "https://store.local/wp-json/wp/v2/product?product_cat=16" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } }, { "id": 21, "count": 1, "description": "", "link": "https://store.local/product-category/decor/", "name": "Decor", "slug": "decor", "taxonomy": "product_cat", "parent": 0, "meta": [], "_links": { "self": [ { "href": "https://store.local/wp-json/wp/v2/product_cat/21" } ], "collection": [ { "href": "https://store.local/wp-json/wp/v2/product_cat" } ], "about": [ { "href": "https://store.local/wp-json/wp/v2/taxonomies/product_cat" } ], "wp:post_type": [ { "href": "https://store.local/wp-json/wp/v2/product?product_cat=21" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } }, { "id": 18, "count": 3, "description": "", "link": "https://store.local/product-category/clothing/hoodies/", "name": "Hoodies", "slug": "hoodies", "taxonomy": "product_cat", "parent": 16, "meta": [], "_links": { "self": [ { "href": "https://store.local/wp-json/wp/v2/product_cat/18" } ], "collection": [ { "href": "https://store.local/wp-json/wp/v2/product_cat" } ], "about": [ { "href": "https://store.local/wp-json/wp/v2/taxonomies/product_cat" } ], "up": [ { "embeddable": true, "href": "https://store.local/wp-json/wp/v2/product_cat/16" } ], "wp:post_type": [ { "href": "https://store.local/wp-json/wp/v2/product?product_cat=18" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } }, { "id": 20, "count": 0, "description": "", "link": "https://store.local/product-category/music/", "name": "Music", "slug": "music", "taxonomy": "product_cat", "parent": 0, "meta": [], "_links": { "self": [ { "href": "https://store.local/wp-json/wp/v2/product_cat/20" } ], "collection": [ { "href": "https://store.local/wp-json/wp/v2/product_cat" } ], "about": [ { "href": "https://store.local/wp-json/wp/v2/taxonomies/product_cat" } ], "wp:post_type": [ { "href": "https://store.local/wp-json/wp/v2/product?product_cat=20" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } }, { "id": 17, "count": 5, "description": "", "link": "https://store.local/product-category/clothing/tshirts/", "name": "Tshirts", "slug": "tshirts", "taxonomy": "product_cat", "parent": 16, "meta": [], "_links": { "self": [ { "href": "https://store.local/wp-json/wp/v2/product_cat/17" } ], "collection": [ { "href": "https://store.local/wp-json/wp/v2/product_cat" } ], "about": [ { "href": "https://store.local/wp-json/wp/v2/taxonomies/product_cat" } ], "up": [ { "embeddable": true, "href": "https://store.local/wp-json/wp/v2/product_cat/16" } ], "wp:post_type": [ { "href": "https://store.local/wp-json/wp/v2/product?product_cat=17" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } }, { "id": 15, "count": 0, "description": "", "link": "https://store.local/product-category/uncategorized/", "name": "Uncategorized", "slug": "uncategorized", "taxonomy": "product_cat", "parent": 0, "meta": [], "_links": { "self": [ { "href": "https://store.local/wp-json/wp/v2/product_cat/15" } ], "collection": [ { "href": "https://store.local/wp-json/wp/v2/product_cat" } ], "about": [ { "href": "https://store.local/wp-json/wp/v2/taxonomies/product_cat" } ], "wp:post_type": [ { "href": "https://store.local/wp-json/wp/v2/product?product_cat=15" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } } ] ```

Reference: https://developer.wordpress.org/rest-api/reference/taxonomies/

355am commented 2 weeks ago

I agree, duplicating functionality isn’t ideal, but unfortunately, the endpoint mentioned (/wp-json/wp/v2/product_cat) isn’t publicly queryable like the others under /wp-json/wc/store, which is causing some issues for my use case. Any suggestions on how to work around this?

edit: also see #42321

mikejolley commented 2 weeks ago

@355am I think you're mistaken there. The read only post type APIs are public. https://woocommerce.com/wp-json/wp/v2/product_cat

355am commented 2 weeks ago

@mikejolley You are right! Interestingly, I tested the endpoint with several WooCommerce webshops and consistently received 404 "rest_no_route" responses. Thank you for providing the link as evidence.

While it works, it would be nice to directly query based on the slug instead of having to make two requests. Is there any chance of introducing slug-based querying in the future?