Libraries (Official) to Work with WooCommerce REST API and Examples
In the examples across my blog I almost always use the HTTP API when create REST API requests to WooCommerce. I do that because my plugins and especially the crossposting plugin uses HTTP API. You would probably agree with me that it is not always the point to include the whole PHP library inside your custom plugin.
But guys, WooCommerce has the amazingly convenient libraries to work with its REST API which you can use on your projects! In this guide we’re going to talk about PHP and JavaScript libraries (but there are also Python and Ruby ones).
Sending Multiple REST API Requests using PHP Requests Library
Recently I got a message from one of my customer and he complained about that inventory sync in my Simple Inventory Sync plugin doesn’t work as fast as he wanted it to.
When I took a look at his website configuration, I noticed that he wanted to syncronise the product stock up to 10 websites at the same time.
At that moment my plugin was already using batch API requests in case someone made an order with 50 products (for example) at the same time (so we can send only a one request instead of 50), but we can not send a single batch request to 10 different sites, can we?
Hooks When Creating or Updating a Post (Classic Editor, Gutenberg and REST API)
Probably all of us know the save_post action hook which usually runs every time a WordPress post of any type (or even a WooCommerce product) is getting created or updated.
But sometimes you need to process only post updates in some specific cases, for example only when you do it via the REST API or using the Classic Editor. Is there a way to do that?
Let’s try to figure it out.
How to Delete a WooCommerce Order Programmatically
In this tutorial I would actually show you how to really delete a WooCommerce order programmatically, I mean because when I tried to find info about it, I only found recommendations about using wp_delete_post() function (which we can’t use it since HPOS appeared) or woocommerce_delete_shop_order() function (there is no such function).
I needed it when I was developing a synced order deletion feature for my Multisite Order Sync plugin.
The long story short, let’s just dive straight into it.
Create a Post with WordPress REST API using JavaScript
In the previous tutorial we discussed how you can create WooCommerce products by sending REST API requests in JS, so I kind of liked this topic and decided to take a deeper look at regular WordPress posts as well, besided there is almost no similar information out there.
The main struggle here of course is to send POST API requests in JavaScript. When you need to get posts, it is simple – just use fetch() and there we go. But when we need to authenticate – how we can do that so nobody can obtain our site application data by just inspecting the code?
How to Get Posts with REST API
If you’ve never worked with WordPress REST API before, then I am definitely recommend you to start with this tutorial, in which we are going to code some basic interactions with REST API using PHP and JavaScript. And as an example we’re just going to get posts from another WordPress blog.
First thing first we need to decide which blog’s posts we are going to get. I can not use my own blog for this example because I have REST API disabled :) I think we can use Matt Mullenweg’s blog for this example who is the founder of WordPress and whose blog has an open REST API (we can easily check it if we type in the browser: ma.tt/wp-json). So, the long story short WordPress REST API allows us to get posts from almost any WordPress blog and to display them either on another website or in a mobile app.
Let’s try to visit this URL then ma.tt/wp-json/wp/v2/posts. You can see that all the latest posts are displayed in your browser tab in JSON format.

And maybe the next question will be – what can we do with this JSON? Ok, right now I am about to show you two examples how to work with it with PHP and JavaScript functions.
How to Update Post Meta with WordPress REST API
This tutorial is an addition to my another tutorial about creating posts using REST API in WordPress, because in that post I didn’t mention how to work with meta fields. So now we’re going to add (or update) meta fields of a specific post with the help of REST API requests.
By the way, the whole process is similar when you work for example with term meta, not only with post meta.