Utilizing the WooCommerce API so as to add {custom} performance


WooCommerce API Information

The WordPress WooCommerce mixture is likely one of the most versatile, user-friendly methods to construct and handle a web-based retailer. A part of what makes the platform so versatile is its APIs. With the APIs, you have got a robust software for dealing with your ecommerce operations and constructing {custom} purposes to handle your corporation.

On this submit, we’ll study how the WooCommerce REST API works and the way you should utilize 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 will probably learn, create, replace, and delete knowledge utilizing JSON requests and customary HTTP verbs. If 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 in your retailer. It’s possible you’ll discover that plugins and further code snippets should not sufficient for creating all of the options that you just want. With the API you’ll be able to pull knowledge and execute requests from your personal custom-made platform.

The API additionally offers you the power 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.


Lower your expenses promoting merchandise, your method

Our Managed WordPress eCommerce consists of greater than $5,000 in free premium WooCommerce extensions, empowering you to sellproducts, your method, with further customization.

Specialised retailer extensions can price tons 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, way more — with free entry to greater than $5,000 in premium WooCommerce extensions.

Study Extra


Listed here 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 should utilize to change the merchandise’s amount or delete it altogether. The API additionally permits you to batch edit your merchandise to make bulk modifications 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 as well 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 need to use these to assessment any metric that you just wish to get a greater perception into your corporation.

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.

If 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’ll be able to create, view, replace, and delete particular person coupons. You may as well modify coupons in batches just like merchandise.

Steps to begin utilizing the WooCommerce API

Earlier than we start, you’ll wish to be sure 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 modifications.

checkbox to enable woocommerce REST API

Create an API key

The WooCommerce API makes use of a digital key-based system for granting entry. If 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 will want to make use of 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.

add API key button in WooCommerce settings

Enter an outline, select a person to personal the API keys, and set the permission ranges. If you’d like the power to create, replace, and delete knowledge, select Learn/Write permissions.

Click on Generate API to create the authentication credentials.

screenshot of WooCommerce API key settings

The subsequent web page will show your client key and client secret. Copy these down as you will want them to attach your API consumer to the WooCommerce REST API.

woocommerce screenshot API, secret key and QR

The second you permit the web page you’ll now not be capable to entry the keys so if you don’t write them down there isn’t any technique to get them again if misplaced.

Hook up with the consumer with API keys

After getting your API keys, the following step is to arrange an API consumer. You’ll use the consumer to make requests and consider responses.

There are a number of shoppers that work effectively with the WooCommerce API. In our instance, we’ll use Postman.

If you open your Postman workspace, both by an online browser or the desktop software, you will note the fields to enter a brand new API request.

Postman API key WooCommerce

Earlier than you can also make the request, you will want to supply the authorization credentials. Click on on Authorization and choose Fundamental Auth for the kind.

Postman Basic Auth settings

Subsequent, you’ll use your API credential to fill within the Username and password. Enter your client key within the Username subject and the buyer secret within the Password subject.

API login info Postman WooCommerce

When you enter your credentials, you might be 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 ready up correctly, the physique of the response ought to comprise the info in your WooCommerce merchandise. The response of this API request is a JSON string that Postman parses for you.

Postman returning JSON response to API request

If one thing is incorrect, chances are you’ll get an error with a 401 or different code. Errors like this will occur. We’ll talk about the right way 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 wish to check a PUT request as effectively to make sure that you do certainly have write entry.

Beneath, we’ll study the right way to replace a WooCommerce product utilizing a PUT request.

To begin, you will want to make the next GET request to fetch the product’s info.

https://yoursite.com/wp-json/wc/v3/merchandise/

For our instance, we’ll use the primary 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

Postman screenshot of WooCommerce API request

To make the PUT request, click on the dropdown menu subsequent to the URL subject and choose PUT.

Sidebar option to select HTTP method

Click on Physique and choose uncooked. Then click on the dropdown arrow for textual content and change the kind to JSON.

JSON response settings in Postman for WooCommmerce API

After this, you’ll be able to enter the request. Beneath is an instance of us altering the worth for our chosen product.

{
"regular_price": "50"
}

Hit ship and it is best to see the modifications mirrored in your WooCommerce web site.

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 should utilize 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 equivalent to shade

{
"id": 1,
"title": "Colour",
"slug": "pa_color",
"sort": "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 assessment

{
"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": "john.doe@instance.com",
"assessment": "Now works simply advantageous.",
"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 simple, problems together with your technical setup and the opposite options that you just use might trigger some points. Beneath are a few of the widespread issues folks face when utilizing the API.

Consumer-agent blocking

The default Consumer Agent for the core HTTP request strategies equivalent to wp_remote_get() makes use of the next format;

WordPress/<>; <>.

Relying in your host or different safety companies that you just use, any requests that reference the WordPress person agent could also be blocked. Whereas meant to guard your WordPress web site from malicious assaults, it might be affecting the API’s capacity to perform correctly.

Authentication headers not handed in CGI Mode

One other widespread problem happens when the Fundamental Authentication header your web site receives in a request doesn’t populate the PHP server variable wanted for WooCommerce to finish fundamental authentication. When this occurs, you will note a 401 error with the message “Unauthorized” or “Shopper secret is invalid.

There are a few methods to deal with this problem. Should you use an Apache server, the very first thing to attempt 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. Should you’re on the lookout for a versatile and highly effective software to take your retailer to the following stage, attempt it out to see how constructive of an impression it may well have in your retailer.

Leave a Reply

Your email address will not be published. Required fields are marked *