Woo Solo Api

I’d like to say — new year, new plugin, but this one was released back in November 2017 xD. Still I’m glad that I can write a bit about my new plugin called Woo Solo Api.

This plugin was a result of a client work here in Croatia. There is an accounting on line service called Solo, and a client I was working for was using their services. One thing they have been missing is a way to connect their API to the WooCommerce store on their site. And the client really wanted to have this ability to have her bill created simultaneously in WooCommerce and in the Solo service. Also one other bit the client wanted was to have the pdf of the Solo bill be sent to the customer with email.

API was there, so connecting it shouldn’t be that hard… Well I had to roam around the WooCommerce code to find the right hook to attach to, and after some testing I had a working version of the plugin.

Nothing is that simple…

First obstacle was testing if all the fields were correctly connected so that the API response doesn’t fail. Also, I needed to make sure to handle any failure. So there were lots of api calls being made from my test environment back and forth until all was correctly set up. Then came the tricky part. How do you send a pdf, if you are making an API call using wp_remote_post ? This is not ajax call, it’s php. No JavaScript whatsoever. Luckily the success response from the Solo API contains a link to the pdf, which you can visit and a pdf will be downloaded. So how do you send an email with a pdf attachment if, when visiting the link with the pdf, it will be automatically downloaded?!

I’ve never done something like this before. This made no sense to me. But there was something I could try. I knew of WordPress filesystem API, maybe I could use this somehow? The idea was this: I have the link that comes from the API response, I could use wp_remote_get , from this I’d get some jumbled code, but I could take the body of this response, and using filesystem create a pdf! I created a directory with all the bills and/or offers, and used $wp_filesystem->put_contents() method to create a pdf. Once this worked (luckily for me it did), I could easily use wp_mail() to send a custom mail to the customer with the pdf attachment.

A lesson learned

In the end I added several more options. I also got some comments what other clients wanted, and because one client didn’t receive the mail with the pdf from the API, I managed to find there was a tiny sneaky bug in my code that prevented the pdf to be sent if the bill type was set to offer. All this is fixed in the 1.2 version.

I also learned quite a bit about using WP_Filesystem, using namespaces in plugins, and a lot more.

Also since my last post, which was in March of 2017, I got a new job as a WordPress engineer at Infinum (April 2017), a superb company which works on producing awesome software. I couldn’t be happier about that. I’ve learned bunch since I started working there Infinum. I also gave my first talk at WordCamp Zagreb, which was a great experience. You can find the presentation of my talk here. All in all, bunch of changes happened. Sadly, I haven’t been able to write tutorials, although I have so much to write about.

Lately I’ve been working on several open source projects at Infinum, one of which is WordPress boilerplate — a theme starter written in object oriented way, using namespaces and autoloading, webpack and all that jazz. I’ve also created a JSON post parser plugin, which parses your post content and stores it in a database as JSON (super useful if you are using decoupled WordPress approach). There are some projects that we are currently working on, and should release them on wordpress.org, hopefully they’ll be out soon as plugins, so keep an eye out on either my twitter, or Infinum’s twitter.

I have a huge article in my draft about using ajax. It’s basically a rewrite of my most popular article here. I hope I’ll find some more time to finish it, and publish it in several parts, so that every part is well covered and explained :)

Hope you like the plugin, and find it useful. If you do, be sure to give it a good rating, or if you find a bug, don’t hesitate to ask in the support so that I can fix it : ) As always feel free to comment below and happy coding : )

Originally published at Made by Denis.