WooCommerce API

Introduction 

 
WooCommerce is a powerful tool for WordPress e-commerce sites. In a time when many different frameworks are used for the site at some point in time, we need a smooth way of handling the data without having to go into the particular page, hence the API. Woocommerce API provides such services for the data that we can manage by using it.
 
Fetch, update, create, edit, delete the customer, orders, coupons, and a lot more through the API.
 
Here are some important headstart guidelines to get started on if the requirements arise in any project.
 
First and foremost the API is very secure and needs authentication. So we have to create credentials.
 
The first step is to activate the API.
 
After logging in as admin, Go to WooCommerce -> settings -> Rest API.
 
Here we have to create the key, select the user, and assign whether these sets of keys should be given read access or both read/write.
 
After this has been completed successfully, we are shown the consumer secret and consumer key. We have to copy both the values as it would show us only once.
 
The next step then is to run the API. The easiest is to go to Github and download the code or using composer install the code in your directory. In both steps, you will need the composer anyway.
 
Here is the link.
 
This page explains pretty much all the steps to connect the API.
  • If you directly download the zip file then you need to extract it and using the command window we have to update the dependencies using the composer. We have to run the command: composer update This much will do and it will update every piece of file it needs.
  • The second steps are the easiest, we just have to go into the directory open the command window and fire the command: composer require Automattic/woo-commerce
After it is done, to make a connection we have to pass 3 requirements which are required. The URL into which we created the keys and the other 2 are two sets of keys.
 
The fourth option is optional in which we can pass some values which vary as per requirements you will get those values on visiting the page above.
 
Here is the example code for getting the latest orders from the site:
  1. <?PHP    
  2. require __DIR__ . '/vendor/autoload.php';    
  3. use Automattic\WooCommerce\Client;    
  4. $woocommerce = new Client(    
  5.     '{URL}',     
  6.     '{consumer_key}',     
  7.     '{consumer_secret}',    
  8.     [    
  9.         'version' => 'wc/v3',    
  10.     ]    
  11. );    
  12. $results = $woocommerce->get('orders');    
  13. echo '<pre><code>' . print_r( $results, true ) . '</code><pre>';    
  14. ?>    
URL will be the site from which we wish to get the data.
 
We make a connection via passing the required values and pass the endpoint to which we want to get the records. In the above example, it is the order’s endpoint.
 
The other such endpoint is $woocommerce->get('customers');
 
Along with this, we can pass the second parameters like orderby, page, offset. They are different based on the endpoints we use.
 
For all the other endpoint details and passing parameters, please visit this link.
 
This link explains all the endpoints that we can use and the parameters it also explains the connection and authentication process.
 
The GitHub link too explains the steps of the same mentioned above. Whichever is suited can be followed.
 

Summary

 
In this article, we have learned about Woocommerce API.
 
I hope that you found this tutorial easy to follow and understand.