This post talks about using WooCommerce, a free and open-source ecommerce plugin for WordPress, but the same principles could be applied to any ecommerce software.
A couple short weeks ago, you allowed me to take a risk and show you several WooCommerce API powered demos live on stage at WooConf 2016.
It went well for the most part, with attendees (both in the audience and online) getting to see a variety of integrations, like sales appearing on a LaMetric display:
To an autonomous drone that took flight when a WooCommerce sale was made:
Side note: How good are David’s GIF skills?!
And while I’m relieved, to be honest, that I don’t have to prepare and perfect that talk any longer, I made a promise on stage. One of my final slides teased at the idea of WooCommerce-powered 3D printing.
I mentioned on stage that this was completely possible and that the only reason I didn’t do a live demo is that a 3D printer doesn’t really fit in carry-on.
The idea of automated 3D printing triggered by WooCommerce sales is very exciting! With several 3d printers at hand, you’re able to automatically 3d print all orders that come in (if your product permits it).
Gone are the days of developing products on paper, sending faxes to China & Taiwan, waiting weeks or months for prototypes to reach your door and then spending the next year perfecting it change by change. We’re standing on the edge of an entirely new industry; of a new timeline, where the speed and level of innovation isn’t determined by a factory’s schedule but by your own unstoppable passion. I’m thrilled by the potential – both of WooCommerce and our store owners – to do great things with this sort of technology in hand.
So is it actually possible? Yes!
How about I show you first before explaining how it all works?
So you can see that a purchase of the coaster starts the 3D printer – success! The printer then takes a few minutes to heat up and align itself. Here’s a time-lapse of the entire print:
Keep in mind, this post is going to get a little bit technical.
My demo today features a variety of items:
- LulzBot Mini 3D Printer (most 3d printers would work though)
- Standard Raspberry Pi
- OctoPrint image or software on the Raspberry Pi
- Woo 3D Print running on your computer
- A WooCommerce store!
The idea here is quite similar to the drone demo at WooConf.
The Woo 3D Print node.js app is running on your local computer. You then tunnel it to the internet, using something like ngrok. WooCommerce is now capable of sending a webhook to the node.js app running locally on your computer, notifying it of a new sale!
If you take a look through app.js in the Woo 3D Print node.js app, you’ll see it starts off by setting a route that excepts POST requests.
This code receives the webhook and checks that both the webhook body and signature header are set. We then move on to checking the secret matches up (in this case I’ve set it to 3d) and parse the webhook body:
At this point, we’re ready to start using the sale’s data. Here we can easily figure out the name of the first item in the order.
Note that we’re replacing spaces in the name with underscores, as we want this name to match up with the corresponding file’s name on the 3d printer.
It’s now time to print!
Here we simply send a POST request to the API that OctoPrint creates for our printer, telling it to print the already-uploaded gcode file with the item’s name.
You’ll need to update the api key set in the request’s header and the IP address of your printer.
The last bit of code is the most important part. It starts the server:
You may want to add an entry to your hosts file (sudo nano /etc/hosts;):
By now you should also have created the webhook in WooCommerce. You can do this under WooCommerce > Settings > API > Webhooks. My webhook looks like this but yours should have a different delivery URL:
The only remaining step is to upload the product’s 3d model to the OctoPrint. You should slice it before into a gcode file (so that the extension is .gco) and name it the same as your product, replacing spaces with underscores.
In the end, we have a pretty nice coaster printed automatically out of a wood filament!