In this tutorial, we will learn about woocommerce REST API for products like how to create, read, update, and delete the products with REST API.
WooCommerce is the most popular eCommerce platform to build any type of online store. WooCommerce has an integrated WordPress REST API. It allows us to create, read, update and delete the products using the requests in JSON format.
The current WP REST API integration version is
v3 which takes the first-order position in endpoints. To use the WP REST API you must have the WordPress 4.4+ and WooCommerce 3.5+ latest version.
WooCommerce Product REST API
The woocommerce products API allows you to create, view, update and delete individual, or multiples, of products.
You have to pass the property’s name and their values in the request and send that to your woocommerce website wp-json REST URL along with API keys.
Generate WC REST API Keys
To get the WooCommerce REST API keys, go to woocommerce’s settings page (WooCommerce > Settings) and then click on the “Advanced” tab. Here you will see the further REST API tab link click on it and you will see the following page.
Now click on the “Add key” or “Create an API key” button to generate the new keys for your woocommerce store. Then add the details about keys and give permission as per need.
When you have generated WooCommerce REST API successfully, now you are ready to use the keys in REST API to authenticate the request in order to create, read, update or delete the products.
Note: Don’t forget to save the keys somewhere else because when you refresh the page the keys will be hidden. And you will not able to see it again though you can change the user, description, and permission later but not API keys.
Create a Product with REST API
Creating a product using the woocommerce REST API is very simple and you can easily do that as you make normal other requests in PHP, Ruby, or NodeJS languages or you can also use the CURL to make requests.
If you are making the request from another wordpress website then you can use the
To create the product in woocommerce using the REST API, we will use the
wp_remote_post() method to send the post request with endpoints and parameters.
In the above code, first, we have taken the $endpoint and then product data fields like name, type, category, image, price, etc.
Then we made a request using the
wp_remote_post() method passing the header to authorize the website (Change the KEY:SECRET with yours) and body data.
wp_remote_retrieve_body() method, return the response and response will be in JSON format. You can print that and make further conditions accordingly.
Read a Product with REST API
Now we will see how to retrieve the product details with woocommerce REST API. We will use
wp_remote_get() method to read the product.
You just have to pass the product ID in the request API URL that you want to read the product and send the request with header authorization. See the following code to get the woocommerce product details using API.
You can see in the above code, we pass the ‘567’ as a product ID in the request URL endpoint and then made the request by passing the API keys.
List all Products with Rest API
To get the list of all the products with woocommerce REST API, you have to make the GET request with /products endpoint. You can also pass the additional parameters in the request for filtering the products.
Some common avaible parameters are
sku. There are more you can check here.
Update a Product with REST API
To update the product using the woocommerce REST API, you have to make the HTTP PUT request by passing the specific product ID.
Let’s say we want to update the price of a product, so we will pass the array data having the new price of the product and send a request.
We sent the POST/PUT request to woocommerce REST API with an authorization header to update the price of a specific product.
Remove a Product with REST API
Now let’s see how to remove the product from woocommerce using the REST API and how to make an HTTP request to delete it.
To delete the wooocommerce product using the REST API, you need to pass the product ID in the request. The request will remove the product from publish the list and move it to the trash list.
Did you notice that we have used the ‘force’ parameters in the delete request? It is used to delete the product permanently without moving it to the trash list.
The default value of the ‘force’ parameter is false, which means that the product will not delete permanently and it will move to the trash list.
So in this tutorial, you learned about woocommerce REST API and how you can create, read, update and delete the products.
As we see how to make HTTP request using the wp_remote_get() and wp_remote_post() methods. You can also use the other third-party libraries to make requests.
Hope this tutorial is helpful for you.