Categories
Online Marketing

WooCommerce REST API Integration

I won’t bore you with any more details but yeah, I’m the owner of a co-owner, cleaner of a chemical data buzz it’s based in Sydney.

I’ve been working as a software developer for almost 30 years now, which makes me feel very old and somewhat wise and experienced for those who not familiar with WordPress. Sorry, where commerce, just quickly it’s a plugin for WordPress, turns your WordPress site into a ecommerce site. So people can buy often pay for stuff. It’s extremely customizable there’s over hundred extensions for WooCommerce alone.

It was founded in September 2011 and it’s now part of the automatic family. I think something like 28 or 30 percent of all ecommerce sites. These days are running. Were commerce so very popular okay, I wanted to talk a bit about my journey, to how I got to here so talking about the integrating with a WooCommerce API, my company, you work with about five or six. So sorry, we’ve got a couple of stores and about five or six years ago the experience wasn’t great, where people would go to a website.

They’ll click, a big PayPal Buy Now button, take them to the PayPal website, make the payment and then we’d get an email, and we would then have to reply to that email send them a taxi invoice attach the software they’d bought. So it was very sort of labor-intensive manual driven, wasn’t great experience if the customer ordered say after nine o’clock on a Friday night senior time. We probably wouldn’t process that until Monday morning, our time as well so over the weekend, they’ll get very anxious, anxious and think they’re being scammed and cancel their critical tasks, sometimes woody ma they’re bought in a bounce-back because they couldn’t receive attachments that big.

So I was all it wasn’t very great here, and so we knew we needed a better solution. So I started around and quickly found we commerce and it worked really well with that. We’re commerce so within short space of time, I’d created to work on websites, and then we had a great experience. Customers could buy software online pay online, get that downloads automatically and get a taxi invoice automatically.

So we didn’t have to we’d have to do a thing. The missing piece for us was use WordPress or e-commerce store in our business. We use it to run our website now, ecommerce stores we use other software like CRM software, and then we have accounting software and we were to get all those orders out of WooCommerce into our CRM and then into our accounting software as well so beans software developers. We looked at knowing that many of you do that WordPress is built in a MySQL back-end.

We thought, oh, let’s just use SQL to get the data. The trouble with that is, it’s there’s no sort of structure around how an order system would typically work. So there’s no orders table, there’s no line items table, there’s no products and customers table like you’d, normally have there’s things like post and post met or not sort of stuff. So you well. We worked out that you could create a an SQL view to get a nice level representation of how I am an order should look that we wanted.

At least we wanted orders with line items that we could say, and that was working quite well, and so one day it stopped working and overnight we couldn’t connect and it took us about sort of one or two weeks to finally track it down. We will he managed to get in touch with a the right person that our web hosting, who said that yes, they’re turned off remote SQL access overnight and though they didn’t tell anyone.

No, they weren’t going to make any exceptions, and thank you for your call. So we’re in we were stuck. We need a solution pretty quickly because we’re to go back to manually processing orders and that we meant a lot of manual. Double data entry and we’ve got better things to do with our time and then have our staff re-enter data. So once again, I started looking around and realized that uh we Wickham is has an API and looking back well like our company.

We live and breathe API so that we spent so working with api’s and looking back, I’m sort of puzzled as to we didn’t just start with the WooCommerce API in the first place. But that’s how we got here so before we get into the details of the WooCommerce API, just a quick bit about those who aren’t familiar with them. So API stands for mission programming interface, there’s the definition from the great Wikipedia I like thinking of it.

Basically, just as a set of instructions about how to get my software talking with it um your software, some of you might have heard the phrase over the last few years about software is eating the world, and so that is API Zurich in the world API. Where these companies like big and small, have API, so we’re press obviously has an API in the accounting, zealand, zero and my bae have have built big platforms around their api’s Tulio’s, a company you might have heard of in the states they all they have is api’s.

They’ve, build a business on api’s around things like SMS messaging and voice messaging and in the social media. Space companies like Twitter and Facebook also have API, so API is everywhere. Ok, so why would you use the word commerce API? So I sort of alluded to it. A bit for many of our customers, it comes down to one word and that integration, it’s about eliminating double data, entry and hassle saving yourself time.

You might have a inventory system that contains all your products and you need to get that into the online store and you don’t have to have your staff changing your internal system. Then we type in all that into your we’re commerce system that leads to sort of data entry errors when you’re doing things twice like our business. Many of our customers have an internal system that got to get their orders into that as well and ultimately into their accounting system.

They’re, probably also, as you do things you can’t do easily in the WooCommerce admin interface itself, so things like bulk updates. So I can change the price of every product or change the description or change the tag and categories and upload them in one go. It sort of the click of one button. We’ve had a couple customers that also want to do things like send their customers to a payment page, and they don’t want after that.

Basically, they want them to not have to go to. The website find some products build an order or add them to the cart and checkout. They just want to sell them to the checkout page. So the API lets us create an order attached for the lot items, generate the payment page link and then just in the customer straight to their payment page, which seems a bit strange. But let’s sell some customers wrong.

Okay, so the benefits of all this, as I mentioned, it’s about saving time, eliminating double data entry, saving your company money when I wake up in the morning – and we had a few orders overnight – and I know that they’ve automatically found their way into our CRM system And ultimately, into our accounting system, and we have enough to do anything – that’s a that’s a great feeling and then gives us that sort of zen-like state of car I’ve been seeking for many years.

So it’s about taking the stress surfer got much better things to do than be data entry clerks all day. So this is what you can achieve with using the API. So let’s get into the details of the the WooCommerce API itself, so there’s the URL for the documentation. It’s a it’s a it’s a very well documented API in our experience to this week was an exciting week in the WooCommerce API space, because a new version was released with Wickham, as 3.

5 has released version 3 of the API. So there’s currently three versions of the API in WooCommerce version, one was for WooCommerce two version. Two was for Wickham s3 and version. Three just came out for Wii commerce. 3.5. You get all three versions: they’re all installed, they’re all enabled you don’t have to install to anything I mean just get them version 1 and version 2. I was going to be deprecated and removed, possibly at some point.

So any development you do today, you did with version 3. So it’s a what’s called a restful api, let’s just quits to the API using standard HTTP verbs that you’ve probably heard of like get post put and deletes and that allows us to create, read, update and delete information or from outside of commerce to use in the Api there’s code samples for a lot of the popular programming language, he’ll, know PHP path and Ruby, and some SDKs that efficient ones you can download and get going with straight away.

So the first thing you normally need to resolve when you’re working with an API for the first time is authentication. How do I connect to the API purely and safely? No then-known, it’s just me and no one else. So the WooCommerce API gives us two options and that depends on how your site served if it’s served over HTTP so know as a sole certificate. They have this both Wampanoag one legged authentication, which is quite convoluted, and I certainly wouldn’t recommend it and that’s what I’m going to talk about it today, because our story of the opinion that you should be serving your site over HTTPS these days, SSL Certificates, extremely cheap And free in some cases and for me, if you’re using WooCommerce, you’re capturing custom information, so that’s what’s called personally identifiable information, you’re capturing orders and you touch into the payment program processes, credit cards or stuff.

I usually should be using HTTPS and if you’re doing that, then authentication is much simpler with the API. You just need to generate a set of API keys using the WooCommerce seven, which I’ll show bit later and they become you could generate. What’s called a consumer key and a consumer secret and they use own password to the API greenshot of that in the Wickham s API. So you go to the Advanced Settings section on REST.

Api create a new key and you’ll, get a screen like that, and you basically copy that consumer key and consumer secret stored in a safe place and that’s how you what you’ll use to authenticate going forward? Okay, so there’s ups there’s four types of requests I’m going to cover, so they are basically create lease and they will have a sort of quite a similar structure. So let’s talk about the correct request, so this is where you want to add some data to WooCommerce.

So it might be a customer or product or an order or category or tag. So in this case you’re going to do an HTTP POST request to an endpoint so that endpoint is customers, products and so on. So you’re going to include your authentication header, which I’ll show in a second and that’s got your assets. That’s how you’ll indicate so. The WooCommerce API is built around Jason as the data structure so to when you’re sending receiving data.

It’s all in JSON, so you’ll need to be able to sort of encode and decode Jason at that point, and that means you need to include the the content type application, Jason header as well you’ll get a JSON response back if it’s all successful and when you Create something you’ll win the work, I’m a Safeco gives you a response. It includes the the ID they work on this ID and you generally want to sort of capture that and store that somewhere, because, if you ever going to touch that record again through the API, whether it’s updating and getting it or deleting it you’ll need to tell We’re commerce which ID that you want to touch so here’s a some using cool from what all my examples.

That’s example in curl, so you can see. I went yeah wait, use my angles, not great for the laser Benham. You can see you don’t HTTP POST request to the customer’s endpoint. You see the version 3 there. That’s how you know which version of a PO you’re referencing, you’ve got user name and password in it’s the consumer key and secret content, type header and then just the JSON data, so email first name last name and username.

Okay, so, following up from the create request, is the update request? It’s basically a famous the Craig request. You do you put instead of a so API world, creating records to put something about updating records. Why some time? First, in this case, where we were, if into the endpoint, but who also reference the ID at the end of the endpoint so which record was ready, we include the same with a same content of jason header and the same jason as well.

Are the only difference doing an update request is that you only need to if you want to send the changes, the things that you know actually update. So, if you’re updating the status of an order, you could just send the order status. You view uploading email address, you just send the email address. It’s up to you now company. We send everything, because that are that you send for a crater and update.

It can be the same. So it’s just easier for us to sort of recycle the codon and do it that way. Okay! So there’s an example of an update request, very similar to the crate request, except it’s just got the ID on the end of the the URL and I’m just including the data. That’s what’s going to be changed, okay, so they’re the two ways you can send data up to WooCommerce. Now we’re going to talk about downloading data from WooCommerce.

So that’s when you do a get request so get rid of any new URL in your browser. There’s data to send you’re, basically just specifying an endpoint and there’s two types of ways you want to download data. You might want to download a single record or a group of records, so we’ll cover the single record first. So when you’re doing a record like a update request, you need to specify the ID of the record that you want to get and you just build out the URL.

You include the authentication header and that’s it you don’t need to send any choice or anything just create the URL and you’ll get back that record. The full Jason check for that record as well. If you wanted to that’s what the response looks like so you can see the ID and the adjacent data for the particular record you’ve requested. The next type of request is when you want to get more than one, so you might want to get all the products in a particular category.

All the orders from last week were yesterday all the tags or the categories whatever might be. So this is where you’re doing is still doing a get request, but obviously specifying an end point say: orders with no ID and you can now build out the URL of it further by including extra parameters and the concept of pagination. So if you look at my little URL here, you can see I’m saying get me all the orders after that date and before that, don’t and give me page one and order them by ID in ascending order.

So let’s talk a bit about pagination. So when I tell WooCommerce to give me lots of Records a list by default, it just gives me ten it. Obviously, if I attend housing orders in Mike Weir commerce site, I wouldn’t want to get those automatically, because that would sort of flood the connection the API would sort of slow to a crawl. My computer probably slowed to a crawl, its processing. That’s so there’s always limits when you’re getting multiple records, so you get ten by default.

You can override that ten by specifying a value for the per page. If you wanted 20 or 50, you can do that. When you get a group of records, you need to examine, what’s called the response, headers and WooCommerce will tell you how many records are found. How many pages are you’re going to need to use to get all those records and it’ll give you the link for the next page as well? So you don’t have to sort of programmatically work out.

What all that stuff is. If we have a look at an example of their response, headers I’ve highlighted some things in red, so at the top is the response code. So anytime you work with an API. You should always get the response code and see if it was a successful response. So 200 series isn’t only the success series. I’ll talk a bit more about that shortly. You can see here. We’ve got the xwp total, so that means 21 means I’ve found 21 records.

Total pages is 3, so I need to make 3 requests if I’m getting 10 per page to get all 21 records there. Hopefully, is the link to the next page, so no need to work out that I can just go and grab that link header and use that for my next page for what, if I’m sort of looping through getting all these and one request? Ok, so the final request I want to cover is the delete request. Delete request is very slimmer to an update or a get single record.

It’s basically specified an endpoint with the ID of the record that you wish to delete you’re, not sending any JSON data you’re. Basically, just doing an HTTP delete request to the endpoint. The only thing you need to be aware of is not all resources support the concept of thrashing and that’s where you can delete a record and it goes into the trash. It’s not permanently deleted. Some of the endpoints and the documentation tells you this.

You need to include an additional parameter. The force equals true and that permanently deletes the record immediately. So you’ll you’ll never get it back when you delete that type of record. Ok, so I wanted to talk a bit about web hooks as well web hooks for those who are not familiar with. Where book is it’s it’s a way of having a notification sent to your server, essentially when a certain action or trigger event occurs so for WooCommerce.

This might be when a customer’s updated or when a order is created, for example, and that’s a screenshot of the web book set up in the world commerce. That means green. Here’s a example of a workflow from our our company and how we use where books. So we now one of our webstore ecommerce web stores gets a new order. We have a web book far off that to our internal server and that creates that contains all the information for that particular order.

But it’s a new order. It’s not a completed order so that all does not doesn’t have all the information that we need right then. So we we wait. We wait us or a moment in time or a couple of minutes, I think for memory, and then we may get a request to the WooCommerce hope you ought to get the latest version of that order, and that includes all the payment information, which is the main Thing that’s missing from when it was first created and then we pulled that back into our internal CRM.

So we’ve got the latest order. So, a couple of months later and then we fire that off using the Xero API, which is our accounting software that we use so within about five minutes to get an order. It’s in our CRM system, the updated version and it’s in our accounting system and that all happens without us having to lift a finger so where books are great. If you want to automate workflows and not have to go and get data from where comments but have a push, you data automatically, ok, so just some tips on sort of picked up over the years of working with various versions of the API.

So, just worth noting time, stamps are always in that ISO 8601 format. As I mentioned earlier, you should always check the HTTP response code, because you want to know where there was a 200 or 201. That’s the successful response codes. You might make a request to get a customer that doesn’t exist, so you’ll still get a successful response paper. Just it’ll take it in for anything, so you need to sort of differentiate between.

Did I get the data that I wanted and versus did. Myopia was my opinion. I request successful, so 400-series are for the sort of request errors. The invalid jason for our one for unauthorized. You’ve got some problem with your api. Keys 500 is a serve error and so on. If you wanted to upload images, say for a product via the api, we’re commerce only lets you upload images by including the full URL for the image.

So it’s going to ingest it and add it to your product when it downloads it. You can also reference if it’s an existing image in your WordPress media library. You can just tell it what the media, library, IDs and it’ll it’ll just link it to product. If you’re wanting to upload a local file, you need to use something like the WordPress API, which does support uploading local files and then grabbing the media ID and adding that to your upload request.

So we’re commerce has got a standard set of fields for things like products and orders and customers, but it might not have all the fields that your customers need for their particular. So a lot of our customers use custom fields for their store and the the way they do. That is through changing the PHP code or using one of the popular custom field plugins. So it’s just worth mentioning that the custom field data that you get isn’t part of the sort of the call will commerce REST API, but they it’s generally including, what’s called the metadata section or the meta data, that’s attached to a product or attached to an order And I’ll show example that immanent yeah, if any, bugs just report them through the github page, they get fixed really quickly.

My experience of reported about half a dozen over the years and they’ve always been fixed and included in the next release of WooCommerce. If you’re not sure where to start with all of this I’d strongly recommend a tool like postman. That’s a free, HTTP client app that you can download and you can use it to construct these requests edge, authentication, header and just see how it all works and examine their response and the response.

Headers and finally, just a bit on the new features of the WooCommerce version through the API that released a couple of days ago. So every time a new version comes out, there’s no menu, endpoints or updates to existing ones so in. But if you win points, so we get a reviews in point. We get a reports in point, a new data endpoint. You can now refund line items through the API. That’s been a sort of a popular request over the years, and you can now edit some things like date, fields on the products endpoint that previously we’re not went.

Editable are the API and when you looking at the documentation, the oppo will tell you if the field is read-only or not. Okay, so I think that’s into my slides. Let’s crack open some demos, okay, so I’m just going to use just a bit of postman first, so this is postman for those who haven’t seen it it’s a free download. You can use it to construct, HTTP requests and just see everything about it. So here’s a little simple example: I’ve got my URL there for my version, three of the products npointing armed taking the products tag, so I want to create a new tag in this example.

So putting my authentication username password, my consumer key and consumer secret and postman does a great job in it generates the authentication header automatically I’ve added in my content, type application, Jason header, so I’m basically ready to go so now. It’s Jason that I’m sending. So I just need to choose the Jason body type and construct some JSON, so I want to create a new tag for my product.

Called products called cake ingredients. I’r just going to click. Send it’s going to request I’m using my little phone here for the Internet. So yes, that worked and it’s come back with a response, so you can see that was successful, there’s my ID, so I could now store that ID number 96 in my particular system, whatever I’m using, and it feels to rename that in the future I can do An update, request and say update ID number 96 and all updated for me.

If you want to look at the headers that come back, you’ll see them here. This was just a create request, so we don’t get the the titles and all that sort stuff with that. So you go to sort of examine the response. Headers that you get back. Let’s look at creating a customer. Now here’s a oops, a customer, oops. Okay. So once again I’m doing a a post, and these are where you choose the different HTTP verbs.

You want to play with so I’m going to do a post to the customers in point. Here’s on my customer data, so let’s just hit Send and that should work okay. So in this case I’ve got an error, and this is how you can tilt you’ve got. I also got a stairs code of 401, so 401 means an authorized, so I’ve also got some problem with my API keys and if you work on, as you know, me gives you a sort of generally a helpful message and you can examine the headers and that Might also tell you a bit more information about what they were always but I’m sort of know from experience to the Foro ones and authorizer.

I will need to go and fix my consumer key and consumer secret okay, so that I can check for an error now. I forgot to actually show you how to create these keys. Let’s jump over to the wordpress. We commerce admin screen. So here I am I’ve just gone to WooCommerce. I’ve gone to settings, advanced REST API and you just click, Add key to create a new key and you’d. Give it a name. Now you choose the user to associate that key with so the sort of the privileges and then you choose.

Is it read, write or or read and write and then, when you generate the key it’ll come back with the consumer key and consumer secret? So at this point you need to copy and paste those and store there somewhere. Secure you’ll, never see these again. At this point, so if you don’t make a note of them, you’ve got to go, go and create another one again, so actually note those down otherwise you’ll need to start again.

Web hooks are also in the same Advanced Settings section. So if you wanted to create in your web hook, you’re just come in here and go add a web hook. Do that again, and yet once again, you’d give it a name and the important thing is choosing the topic or what’s the trigger or what’s the you want, this web hook to fire on? So these are the the predefined options that we commerce gives us so generates about when something was created or updated.

So I that’s the order. Credit Amun, that’s the one that we use in our business, but you also get some other ones around products as well. Okay, so let’s switch over to FileMaker, so we do a lot of work, as was mentioned earlier, I’m in the FileMaker platform. It’s a popular platform, small businesses, it’s Apple and it’s great. It runs on to the Mac Windows my on the web, and you can use it to integrate with lots of API, so we use a to integrate with that we’re comments in particular, so I might want to download all my orders for the month of October.

For example, so here I’m doing a requested on filtering by date range and I’ll click that button say get me all the orders between those two dates. So it’s getting me a list, so I’m getting 10 at a time and it’s just sort of working his way through them. It’s happening over my mobile phone, so it might be a tiny bit slow and yeah. So this was my internal system of now got all my orders in here. I can then go and modify them print.

Shipping labels and anything else I want, might want to do my sort of CRM system and then I can send them emails and send it off to my accounting system as well. I think that was all I had for my demo, so we just switch back to my slides, yeah. Okay, so there’s my contact details so I’ll be around for a couple of days. If you have any questions, but now know what it’s like to open up for questions,