The WordPress WooCommerce mixture is without doubt one of 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 might have a robust software for dealing with your ecommerce operations and constructing {custom} purposes to handle your small business.
On this submit, we’ll look at how the WooCommerce REST API works and the way you need to use it to enhance your ecommerce retailer.
What are you able to do utilizing the WooCommerce API?
The WordPress REST API permits outdoors purposes to work together with WordPress’s performance. The WooCommerce API is a totally built-in extension of the WordPress REST API. It might learn, create, replace, and delete knowledge utilizing JSON requests and commonplace 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 begin, the API is a robust software for implementing {custom} performance to your retailer. It’s possible you’ll discover that plugins and further code snippets aren’t sufficient for creating all of the options that you simply want. With the API you’ll be able to pull knowledge and execute requests from your personal custom-made platform.
The API additionally provides you the power to decouple the frontend and backend of your web site. By doing so, you need to use one other platform or language to create pages and designs whereas utilizing WordPress to energy your backend.
Get monetary savings promoting merchandise, your approach
Our Managed WordPress eCommerce consists of greater than $5,000 in free premium WooCommerce extensions, empowering you to sellproducts, your approach, with extra customization.
Specialised retailer extensions can value lots of, even 1000’s of {dollars} per 12 months when you get all of them added to your retailer.
However with our Managed WordPress eCommerce, you’ll be able to construct the precise retailer you wanted to promote subscriptions, e-book appointments, allow digital downloads and far, far more — with free entry to greater than $5,000 in premium WooCommerce extensions.
Listed below are a few of the operations you’ll be able to carry out utilizing the API.
Including updating and deleting merchandise
There are endpoints for single merchandise, attributes, and variations that you need to use to switch the merchandise’s amount or delete it altogether. The API additionally enables you to batch edit your merchandise to make bulk adjustments sooner.
Processing orders
With the API, you’ll be able to course of your orders individually or in bulk by updating statuses and making use of monitoring particulars. You may also use the API to retrieve a person order or an inventory of all of your orders.
Creating dashboards
The WooCommerce API lets you create {custom} dashboards and interfaces. You need to use these to evaluation any metric that you simply wish to get a greater perception into your small business.
Viewing settings
If you wish 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 include every of your WooCommerce settings teams and their present parameters.
Creating coupons
With the coupons API, you’ll be able to create, view, replace, and delete particular person coupons. You may also modify coupons in batches just like merchandise.
Steps to begin utilizing the WooCommerce API
Earlier than we start, you’ll wish to ensure 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, verify the field and save the adjustments.
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 may be given a client key and client secret. Every time you make a name to the API, you have to to make use of these keys for authentication. This course of helps be 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 would like the power to create, replace, and delete knowledge, select Learn/Write permissions.
Click on Generate API to create the authentication credentials.
The following web page will show your client key and client secret. Copy these down as you have to them to attach your API shopper to the WooCommerce REST API.
The second you allow the web page you’ll now not have the ability to entry the keys so if you don’t write them down there is no such thing as a technique to get them again if misplaced.
Connect with the shopper with API keys
After you have your API keys, the subsequent step is to arrange an API shopper. You’ll use the shopper to make requests and think about responses.
There are a number of purchasers that work properly with the WooCommerce API. In our instance, we are going to use Postman.
While you open your Postman workspace, both by way of an internet browser or the desktop software, you will notice the fields to enter a brand new API request.
Earlier than you may make the request, you have to to offer the authorization credentials. Click on on Authorization and choose Primary Auth for the sort.
Subsequent, you’ll use your API credential to fill within the Username and password. Enter your client key within the Username area and the buyer secret within the Password area.
When you enter your credentials, you’re able to make your first request. For our walkthrough, let’s start with a request to listing all merchandise.
Use the next GET request, substituting your area for the placeholder
https://yoursite.com/wp-json/wc/v3/merchandise
If the configuration is about up correctly, the physique of the response ought to include the information to your WooCommerce merchandise. The response of this API request is a JSON string that Postman parses for you.
If one thing is incorrect, you might get an error with a 401 or different code. Errors like this may occur. We’ll focus on how one can handle a few of these a bit in a while.
Begin making API requests
After you make your first profitable GET request, you’ll wish to take a look at a PUT request as properly to make sure that you do certainly have write entry.
Beneath, we are going to look at how one can replace a WooCommerce product utilizing a PUT request.
To begin, you have to to make the next GET request to fetch the product’s data.
https://yoursite.com/wp-json/wc/v3/merchandise/
For our instance, we are going to use the primary product consequence 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 sort to JSON.
After this, you’ll be able to enter the request. Beneath is an instance of us altering the value for our chosen product.
{ "regular_price": "50" }
Hit ship and it’s best to see the adjustments mirrored in your WooCommerce website.
Examples of WooCommerce API requests
Now that we now have efficiently made each GET and PUT requests, let’s check out a few of the helpful requests you need to use to handle your WooCommerce retailer by way of 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 transport handle
{ "transport": { "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 akin to coloration
{ "id": 1, "title": "Shade", "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 evaluation
{ "id": 20, "date_created": "2018-09-08T21:47:19", "date_created_gmt": "2018-09-09T00:47:19", "product_id": 31, "standing": "accepted", "reviewer": "Claudio Sanches", "reviewer_email": "john.doe@instance.com", "evaluation": "Now works simply tremendous.", "ranking": 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 easy, problems together with your technical setup and the opposite options that you simply use could trigger some points. Beneath 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 akin to wp_remote_get() makes use of the next format;
WordPress/<>; <>.
Relying in your host or different safety providers that you simply use, any requests that reference the WordPress consumer agent could also be blocked. Whereas supposed to guard your WordPress website from malicious assaults, it may very well be affecting the API’s means to operate correctly.
Authentication headers not handed in CGI Mode
One other widespread concern happens when the Primary Authentication header your website receives in a request doesn’t populate the PHP server variable wanted for WooCommerce to finish fundamental 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 concern. If you happen to use an Apache server, the very first thing to strive is including the next 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. If you happen to’re in search of a versatile and highly effective software to take your retailer to the subsequent degree, strive it out to see how optimistic of an affect it will possibly have in your retailer.