Are you looking to interact with the WooCommerce REST API? Probably you are not using WordPress on the front end but want to place a store on your website. In such cases, you can use WooCommerce as a backend to manage your store’s data. WooCommerce provides a REST API to provide real-time data from the backend. This data can be placed anywhere on your front-end application. One can manage their store data by hitting the WooCommerce REST API endpoints.
In this article, we study how to use WooCommerce REST API. I’ll explain calling WooCommerce endpoints using PHP and JavaScript.
WooCommerce is one of the popular eCommerce platforms for WordPress. Additionally, it provides REST API services, so everyone can use it out of WordPress.
With WooCommerce one can manage almost everything required to run an online store. Whether it be products, customers, coupons, product images, product attributes, etc. all these things are available in WooCommerce. For this tutorial, I am going to interact with the Product endpoints. In a similar way, you can integrate other options like customers, orders, etc.
Get Your Consumer Key and Consumer Secret
To get started with WooCommerce REST API, you first need to get your consumer key and consumer secret values. These keys are the identification for your store and are necessary while giving an API call to WooCommerce. You must send a base64 encoded string of these keys in the Authorization header.
Note: It’s highly recommended to have an eCommerce site running on HTTPS. But, if you are testing it on a local server then instead of WooCommerce API keys use the application password provided by WordPress. Follow the linked article where I explain how to use it with REST API.
On the WordPress dashboard, head over to the WooCommerce->Settings page. Click on the Advanced tab and then on the REST API link.
On this page click on the button ‘Create an API Key’.
Enter the description, choose ‘Read/Write’ permissions, and hit the Generate API key button. You can also choose Read permission. It depends on your requirements. If you just need to read data from WooCommerce then choose the ‘Read’ option. In case you wish to run both reads and write operations, your option would be ‘Read/Write’.
Copy your consumer key and consumer secret which will be required in the later step.
Use WooCommerce REST API in PHP
Once you are ready with your consumer key and consumer secret, we can start with the code which interacts with WooCommerce. As we need to send HTTP requests, install the Guzzle library using the command below.
composer require guzzlehttp/guzzle
Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services.
Now let’s integrate some endpoints like listing products, getting a single product, and updating a product.
List Products
In the below code, I hit the WooCommerce endpoint given to get a list of products. To execute this endpoint successfully, I am passing a base64 encoded string of the consumer key and consumer secret in the Authorization header.
<?php
require_once "vendor/autoload.php";
use GuzzleHttp\Client;
define('WC_CONSUMER_KEY', 'PASTE_CONSUMER_KEY');
define('WC_CONSUMER_SECRET', 'PASTE_CONSUMER_SECRET');
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'YOUR_DOMAIN_BASE_URL',
]);
try {
$response = $client->request('GET', '/wp-json/wc/v3/products', [
'headers' => [
"Authorization" => "Basic ". base64_encode(WC_CONSUMER_KEY.':'.WC_CONSUMER_SECRET)
],
'query' => [
'per_page' => 18,
],
]);
$body = $response->getBody();
$arr_body = json_decode($body);
print_r($arr_body);
} catch (Exception $e) {
echo $e->getMessage();
}
Before running a code, replace the placeholders with the actual values. Here, I have passed 18 as a ‘per_page’ value which will return 18 products in response. You can pass this value as per your requirement.
Get a Single Product
To fetch a single product your endpoint will be as follows.
$response = $client->request('GET', '/wp-json/wc/v3/products/PRODUCT_ID_HERE', [
'headers' => [
"Authorization" => "Basic ". base64_encode(WC_CONSUMER_KEY.':'.WC_CONSUMER_SECRET)
],
]);
Update a Product
You can update a product by sending a PUT request to the API endpoint. Let’s say you want to update the regular price of your product.
$response = $client->request('PUT', '/wp-json/wc/v3/products/PRODUCT_ID_HERE', [
'headers' => [
"Authorization" => "Basic ". base64_encode(WC_CONSUMER_KEY.':'.WC_CONSUMER_SECRET)
],
'json' => [
'regular_price' => '12.20',
],
]);
This is how you can use WooCommerce REST API for managing products. In the same way, the user can send HTTP requests for Customers, Orders, Coupons, etc. Read more about it on official documentation.
Use WooCommerce REST API with JavaScript
For the website built with the JavaScript framework, you may use the Fetch API to interact with the WooCommerce REST API endpoints. As an example, I write the code to get a list of products. You should adjust the code as per the endpoints.
I’ll use create a base64 encoded string using the btoa()
method in JavaScript.
<script>
var consumer_key = 'PASTE_CONSUMER_KEY';
var consumer_secret = 'PASTE_CONSUMER_SECRET';
fetch('DOMAIN_URL/wp-json/wc/v3/products', {
headers: {Authorization: "Basic " + btoa(consumer_key + ':' + consumer_secret)}
})
.then(response => response.json())
.then(data => {
console.log(data);
});
</script>
Conclusion
In this tutorial, we study calling the WooCommerce REST API using PHP and JavaScript. We went through a few API endpoints of WooCommerce. The user can play with the other endpoint following the official documentation.
Related Articles
- How to Display Related Products Using WooCommerce REST API
- How to Integrate Mailchimp with WooCommerce
- Customize WooCommerce Product Search On Your WordPress Site
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.