Using the WooCommerce API to add custom functionality

The WordPress WooCommerce mixture is one among the most versatile, user-friendly methods to construct and handle an internet retailer. A part of what makes the platform so versatile is its APIs. With the APIs, you will have a strong instrument for dealing with your ecommerce operations and constructing custom functions to handle your enterprise.
On this submit, we’ll look at how the WooCommerce REST API works and the way you should utilize it to enhance your ecommerce retailer.
Table of Contents
- What are you able to do utilizing the WooCommerce API?
- Get monetary savings promoting merchandise, your means
- Including updating and deleting merchandise
- Processing orders
- Creating dashboards
- Viewing settings
- Creating coupons
- Steps to begin utilizing the WooCommerce API
- Allow API entry
- Create an API key
- Join to the consumer with API keys
- Begin making API requests
- Examples of WooCommerce API requests
- Get requests
- GET all orders
- GET all merchandise
- GET all prospects
- GET all product classes
- GET a single product
- GET a product variation
- GET a single buyer
- Put requests
- Replace buyer’s delivery handle
- Replace product worth and inventory amount with a single request
- Replace product attributes reminiscent of shade
- Replace worth of product variant
- Replace buyer’s first and final title
- Replace a five-star product evaluate
- Resolving points
- Person-agent blocking
- Authentication headers not handed in CGI Mode
- Conclusion
What are you able to do utilizing the WooCommerce API?
The WordPress REST API permits outdoors functions to work together with WordPress’s functionality. The WooCommerce API is a completely built-in extension of the WordPress REST API. It might learn, create, replace, and delete information utilizing JSON requests and customary HTTP verbs. While you make a REST API name, WooCommerce and WordPress join with the PHP and SQL backend. They then course of your request and generate the output.
With endpoints uncovered for primarily each a part of your WooCommerce retailer, there’s a lot you are able to do with the API.
To start out, the API is a strong instrument for implementing custom functionality in your retailer. You could discover that plugins and further code snippets are usually not sufficient for creating all the options that you just want. With the API you may pull information and execute requests from your personal custom-made platform.
The API additionally offers you the means to decouple the frontend and backend of your web site. By doing so, you should utilize one other platform or language to create pages and designs whereas utilizing WordPress to energy your backend.
Get monetary savings promoting merchandise, your means
Our Managed WordPress eCommerce consists of greater than $5,000 in free premium WooCommerce extensions, empowering you to sellproducts, your means, with extra customization.
Specialised retailer extensions can price lots of, even 1000’s of {dollars} per yr when you get all of them added to your retailer.
However with our Managed WordPress eCommerce, you may construct the actual retailer you wanted to promote subscriptions, ebook appointments, allow digital downloads and far, way more — with free entry to greater than $5,000 in premium WooCommerce extensions.
Be taught Extra
Listed below are a few of the operations you may carry out utilizing the API.
Including updating and deleting merchandise
There are endpoints for single merchandise, attributes, and variations that you should utilize to modify the merchandise’s amount or delete it altogether. The API additionally enables you to batch edit your merchandise to make bulk modifications quicker.
Processing orders
With the API, you may course of your orders individually or in bulk by updating statuses and making use of monitoring particulars. You can too use the API to retrieve a person order or an inventory of all of your orders.
Creating dashboards
The WooCommerce API permits you to create custom dashboards and interfaces. You should use these to evaluate any metric that you really want to get a greater perception into your enterprise.
Viewing settings
If you’d like to view and handle your WooCommerce settings from a custom backend platform you are able to do so utilizing the API.
While you make a GET request, the JSON response will comprise every of your WooCommerce settings teams and their present parameters.
Creating coupons
With the coupons API, you may create, view, replace, and delete particular person coupons. You can too modify coupons in batches related to merchandise.
Steps to begin utilizing the WooCommerce API
Earlier than we start, you’ll need to make sure that entry to the WooCommerce API is enabled inside WordPress.
Allow API entry
Go to WooCommerce > Settings > Superior > Legacy API. Look to see is the API is enabled. If not, test the field and save the modifications.
Create an API key
The WooCommerce API makes use of a digital key-based system for granting entry. While you create a brand new API connection, you can be given a shopper key and shopper secret. Every time you make a name to the API, you will have to use these keys for authentication. This course of helps make sure that all API requests are legit.
To create a brand new API key go to WooCommerce > Settings > Superior > REST API and click on Add key.
Enter an outline, select a consumer to personal the API keys, and set the permission ranges. If you’d like the means to create, replace, and delete information, select Learn/Write permissions.
Click on Generate API to create the authentication credentials.
The following web page will show your shopper key and shopper secret. Copy these down as you will have them to join your API consumer to the WooCommerce REST API.
The second you permit the web page you’ll now not have the ability to entry the keys so if you don’t write them down there isn’t a means to get them again if misplaced.
Join to the consumer with API keys
After you have your API keys, the subsequent step is to arrange an API consumer. You’ll use the consumer to make requests and think about responses.
There are a number of shoppers that work nicely with the WooCommerce API. In our instance, we are going to use Postman.
While you open your Postman workspace, both by an internet browser or the desktop software, you will notice the fields to enter a brand new API request.
Earlier than you can also make the request, you will have to present the authorization credentials. Click on on Authorization and choose Fundamental Auth for the kind.
Subsequent, you’ll use your API credential to fill in the Username and password. Enter your shopper key in the Username area and the shopper secret in the Password area.
When you enter your credentials, you’re prepared to make your first request. For our walkthrough, let’s start with a request to checklist all merchandise.
Use the following GET request, substituting your area for the placeholder
https://yoursite.com/wp-json/wc/v3/merchandise
If the configuration is ready up correctly, the physique of the response ought to comprise the information in your WooCommerce merchandise. The response of this API request is a JSON string that Postman parses for you.
If one thing is unsuitable, you could get an error with a 401 or different code. Errors like this could occur. We are going to focus on how to handle a few of these a bit in a while.
Begin making API requests
After you make your first profitable GET request, you’ll need to take a look at a PUT request as nicely to make sure that you do certainly have write entry.
Under, we are going to look at how to replace a WooCommerce product utilizing a PUT request.
To start out, you will have to make the following GET request to fetch the product’s info.
https://yoursite.com/wp-json/wc/v3/merchandise/
For our instance, we are going to use the first product end result from our earlier request. This merchandise has an id of 101 so our request URL is;
https://yoursite.com/wp-json/wc/v3/merchandise/101
To make the PUT request, click on the dropdown menu subsequent to the URL area and choose PUT.
Click on Physique and choose uncooked. Then click on the dropdown arrow for textual content and swap the kind to JSON.
After this, you may enter the request. Under is an instance of us altering the worth for our chosen product.
{ "regular_price": "50" }
Hit ship and it’s best to see the modifications mirrored in your WooCommerce website.
Examples of WooCommerce API requests
Now that we have now efficiently made each GET and PUT requests, let’s check out a few of the helpful requests you should utilize to handle your WooCommerce retailer through the API.
Get requests
GET all orders
https://yoursite.com/wp-json/wc/v3/orders
GET all merchandise
https://yoursite.com/wp-json/wc/v3/merchandise/
GET all prospects
https://yoursite.com/wp-json/wc/v3/prospects/
GET all product classes
https://yoursite.com//wp-json/wc/v3/merchandise/classes
GET a single product
https://yoursite.com/wp-json/wc/v3/merchandise/{product ID}
GET a product variation
https://yoursite.com/wp-json/wc/v3/merchandise/{product ID}/variations/{variation ID}
GET a single buyer
https://yoursite.com/wp-json/wc/v3/prospects/{buyer ID}
Put requests
Replace buyer’s delivery handle
{ "delivery": { "first_name": "Jane", "last_name": "Doe", "firm": "Jane Doe Co", "address_1": "361 Exterior Rd", "address_2": "Suite 101", "metropolis": "Las Vegas", "state": "NV", "postcode": "89102", "nation": "US" } }
Replace product worth and inventory amount with a single request
{ "regular_price": "50", "stock_quantity": 30 }
Replace product attributes reminiscent of shade
{ "id": 1, "title": "Colour", "slug": "pa_color", "kind": "choose", "order_by": "title", "has_archives": true, "_links": { "self": [ { "href": "https://example.com/wp-json/wc/v3/products/attributes/6" } ], "assortment": [ { "href": "https://example.com/wp-json/wc/v3/products/attributes" } ] } }
Replace worth of product variant
To replace a product or product variant’s “worth” or “regular_price”, here’s a instance PUT request:
{ "regular_price": "81" }
Replace buyer’s first and final title
{ "first_name": "John", "last_name": "Doe" }
Replace a five-star product evaluate
{ "id": 20, "date_created": "2018-09-08T21:47:19", "date_created_gmt": "2018-09-09T00:47:19", "product_id": 31, "standing": "accredited", "reviewer": "Claudio Sanches", "reviewer_email": "[email protected]", "evaluate": "Now works simply positive.", "score": 5, "verified": true, "reviewer_avatar_urls": { "24": "https://safe.gravatar.com/avatar/908480753c07509e76322dc17d305c8b?s=24&d=mm&r=g", "48": "https://safe.gravatar.com/avatar/908480753c07509e76322dc17d305c8b?s=48&d=mm&r=g", "96": "https://safe.gravatar.com/avatar/908480753c07509e76322dc17d305c8b?s=96&d=mm&r=g" }, "_links": { "self": [ { "href": "https://example.com/wp-json/wc/v3/products/reviews/20" } ], "assortment": [ { "href": "https://example.com/wp-json/wc/v3/products/reviews" } ], "up": [ { "href": "https://example.com/wp-json/wc/v3/products/31" } ], "reviewer": [ { "embeddable": true, "href": "https://example.com/wp-json/wp/v2/users/1" } ] } }
Resolving points
Whereas utilizing the WooCommerce API is comparatively simple, problems along with your technical setup and the different options that you just use might trigger some points. Under are a few of the widespread issues folks face when utilizing the API.
Person-agent blocking
The default Person Agent for the core HTTP request strategies reminiscent of wp_remote_get() makes use of the following format;
WordPress/<>; <>.
Relying in your host or different safety providers that you just use, any requests that reference the WordPress consumer agent could also be blocked. Whereas meant to defend your WordPress website from malicious assaults, it could possibly be affecting the API’s means to operate correctly.
Authentication headers not handed in CGI Mode
One other widespread problem happens when the Fundamental Authentication header your website receives in a request doesn’t populate the PHP server variable wanted for WooCommerce to full primary authentication. When this occurs, you will notice a 401 error with the message “Unauthorized” or “Shopper secret is invalid.
There are a few methods to handle this problem. In case you use an Apache server, the very first thing to attempt is including the following modification to your .htaccess file.
# BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule ^index.php$ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress
Conclusion
This submit has scratched the floor on what you are able to do with the WooCommerce API. In case you’re on the lookout for a versatile and highly effective instrument to take your retailer to the subsequent degree, attempt it out to see how optimistic of an impression it might have in your retailer.