Last update March 23, 2023 at 05:25 am
In this article we will see how to use the woocommerce REST API to have all the products on our client.
We are currently developing an android application for one of our clients.
One of the important features was to retrieve all the products from its woocommerce site. The problem is that with the platform's new REST API standard, a lot of tricks on the Web are no longer functional.
In this troubleshooting we will give you the solution that allowed us to solve the problem and some tips so you don't get lost.
This article is not a tutorial but a troubleshooting section. We are therefore not going to go too much into the details of the instructions and give detailed explanations of the problem encountered. The goal is to get straight to the point to unblock yourself.
Before offering you the solution there are steps to configure your application customer and the woocommerce platform to accomplish to be sure to be on the right track.
We are going to try to give you prerequisites which could perhaps be a solution to the non-operation of your REST client. Let's start:
- You will need to generate a connection key before using the API. To do this you will need to go to Woocommerce -> Settings -> Advanced -> REST API See the documentation on the woocommerce site to guide you (link https://docs.woocommerce.com/document/woocommerce-rest-api/). Be careful, save the generated keys elsewhere before generating it. Otherwise you will be forced to delete what you have just created and start the process again.
- Check that the link: my_site_woocommerce.com// wp-json / wc / v3 / products? consumer_key =customer_key& consumer_secret =secret_key is functional.
- NB: In production never put the authentication information as the client key and the secret key of your platform in a hard url in your client application. It is better to manage the authentication by modifying with your programming language the protocol of the request for more security. Each programming language has its way of handling this procedure correctly.
With android using the kotlin or java language, you can manage authentication using interceptors and OkHTTP. These details will be the subject of tutorials later. If everything works well then you can go directly to the solution.
Solution to find all the products on my Woocommerce site via the REST API
To find all the products on your woocommerce site, you must apply filters so as not to mix up unpublished products with those you are really looking for.
In our case, we are only interested in products having at least the price, and being visible to the public. For this our solution is to use the following url:
mysite_woocommerce.com// wp-json / wc / v3 /products? status = publish & per_page = 100 & min_price = 1
The product display limit is 100 per page. If we want to recover more, we will have to manage the pagination and add a new parameter. To have all the details of using the API, I recommend that you see this REST API documentation.
This documentation is very complete and allows you to more simply test your queries as you modify the parameters.
This is the end of help on the recovery of all the products on our woocommerce by a request to the REST webservice.
The reason you can't fetch 100 products at a time is that after a number of request processing times, the connection is automatically reset.
To avoid this situation, make queries that require less time to run.