Fetch products by category in Woocommerce REST API

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:

Read More
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.

Related posts

Leave a Reply

9 comments

  1. 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.

  2. 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}

  3. 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
    
  4. 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
    
  5. 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.