Also see our article on how to sync WooCommerce products from an API.
Our PRO plugin includes a feature called Actions that allows you to send WooCommerce data (and other WordPress data) to your API without writing a single line of code.
This allows you to easily integrate your WooCommerce store with any REST API and trigger your API calls when you get a new order, add an order from the admin area, create a new product or change the status of a product or an order.
Let’s look at how some of these Actions work.
Firstly you need to setup the endpoint that you want to send the data to. If you have not done that, you can view the Quick Start guide here.
Call API on new WooCommerce order
To call your API when you get a new WooCOmmerce order, you simply need to select WooCommerce – New Order Created from the dropdown within your endpoint. Save the endpoint and now any time a new WooCommerce order is created, this endpoint is called.
Call API when WooCommerce order status changes
Within your endpoint, you simply select WooCommerce – Order Status Changed from the dropdown.
After selecting that option, you will see the Old Order Status and New Order Status options. This allows you to fine tune when you want to call this API. In our example we are only calling this endpoint when the status changes from ‘Any Status’ to ‘Completed’ status.
We can save this endpoint and now any time a WooCommerce order is changed to ‘Completed’ this endpoint is called.
Send WooCommerce order data to the API
Now that we have seen how to setup the order Actions, we will most likely want to send this order data to the API. To do this, we use a ‘Token’ within the endpoint settings page that grabs any piece of data related to a particular order.
In the screenshot below we are sending a few details about the order to the API within the Body POST Fields. It is simply sending the order date, the total of the order and the order_key. We are doing this by using tokens such as (action:total).
Each token within Action starts with (action: and then the name of the data that we want to get.
You can do a test to see the full list of data available by:
- creating a new order in your admin (or frontend)
- check the WPGetAPI > Actions Log page which contains all the data that is available to send
Please also see our docs on Actions for a more comprehensive write up.
Getting all line items
If you wanted to only get the SKU of the first product purchased within an order, you can use this exact token: (action:line_items:0:sku)
But usually the number of products within an order will be dynamic and will often change. There could be one product or there could be 10. To overcome this and to be sure that we grab the details of each product in an order, we have a special (line_items) token we can use that looks something like this:
(line_items:start)
(get:sku)
(get:qty)
(get:subtotal)
(get:image_url)
(line_items:end)
And here is what it looks like within the admin of the endpoint settings page, with some extra JSON formatting thrown in such as commas, quotes and curly braces:
Call API when WooCommerce product is created
Within your endpoint, you simply select WooCommerce – New Product Created from the dropdown.
Sending product data to API
Now once this action happens, we are able to grab any data we like that is related to that particular product.
In the screenshot below we are sending a bunch of different details about the product that was just created. You can add unlimited details about the product.
For a full list of the data available that you can send, please see our docs on Actions. You can also test this by adding a new product in your admin and then checking the WPGetAPI Action Log which contains all the data available to send.