Reputation: 744
I'm using Woocomerce for my wordpress shopping cart. I'm connect my Android App to my Wordpress database through Woocommerce REST API. I need to get products by category on my App. There is no provision in the REST API to fetch product by category.
I tried by using the filter attribute:
https://example.com/wc-api/v1/products?filter[product_cat]=gedgets&consumer_key=ck_9354534x&consumer_secret=cs_dx7345345
This is return me all the products.
Upvotes: 8
Views: 28214
Reputation: 69
if you are looking products filter by multiple categories id use below API endpoint:
https://example.com/wp-json/wp/v2/posts?categories=20,30
Upvotes: 2
Reputation: 41
add key and use Consumer Key and Consumer Secret for type OAuth 1.0 authorization header and use this API.
I used this API and it's perfectly working
Upvotes: 4
Reputation: 1620
Just in case anyone is trying to achieve this in the future with v2 of the Woo REST API, this is how it's done:
https://${baseUrl}/wp-json/wc/v2/products?category=${categoryId}
And if you only have the slug of the category, as opposed to the id, you can first:
https://${baseUrl}/wp-json/wc/v2/products/categories?slug=${categorySlug}
Upvotes: 3
Reputation: 1819
In this case you should use the filters...
however, It worked for me :
http://www.example.com/wp-json/wc/v1/products?category=16
the 16 is a sample of category id...
Upvotes: 12
Reputation: 370
use filter[category] insted of filter[categories]
https://ursite.com/wc-api/v1/products?filter[category]=gedgets&consumer_key=ck_9354534x&consumer_secret=cs_dx7345345
Upvotes: 2
Reputation: 4204
Actually, according to the github page, this feature is not yet available. However, if you look here: http://justgeeks.in/woocommerce-rest-api-fetch-products-category/ you will find what you need to filter by category slug...
Upvotes: 0
Reputation: 744
According to the Woocommerce REST API, They don't have a solution(filters) for this issue. woocommerce github
Hope they will add this feture in their next release.
Upvotes: 0
Reputation: 89
Could it be a typo in your filtering? I noticed that it says [categories]=gedgets but should it be [categories]=gadgets instead?
I am trying to figure out Woocommerce REST API myself and I noticed that if I type something wrong in the filter, it returns all results.
Upvotes: 0
Reputation: 8877
According to the WooThemes/WooCommerce documentation you need to use filters
, like you say, only you appear to have the filter name wrong.
Try:
https://mysite.com/wc-api/v1/products?filter[categories]=gedgets&consumer_key=ck_9354534x&consumer_secret=cs_dx7345345
categories
is the name of the field returned by the API, so that should be what you use.
Upvotes: 7