How to do Fulfillments
So your shop is a raging success. You are receiving upwards of 100, 200 maybe even 454 orders per day for your popular product. You are in business and the success of getting to this point is keeping you flush with excitement.
You are like many merchants operating in the e-commerce realm and you have your shop inventory stored with company MaxiFulfillments LLC. Shopify made it easy for your orders to make their way to that company and they happily process your orders when you hit the fulfillment button. Thing is, you don't want to do that 100, 200 or 454 times a day. And to boot, even though the fulfillment company is great at shipping and dealing with your inventory, they only provide you with tracking numbers via an Excel spreadsheet that comes in your email box at the end of each day. You are loath to manually go through that file and paste 100, 200 or 454 tracking numbers into your orders and then close them. But damn, with close to 5000 open orders and no end in sight, it is time to do something about that.
Leverage the Cloud. Dropbox to the Rescue Direct link to this section
When a merchant receives a spreadsheet with rows and rows of data to try and incorporate into their order management they quickly notice there is no where to upload this data. So I create a Dropbox App for them that they can use for this purpose. We use the same authentication as Shopify Apps use, namely oAuth, and this time we setup the provider as Dropbox. Using the App, they navigate to the Dropbox install option and it pops up the Dropbox App installer screen much like Shopify Apps present to the merchant. When they approve the installation of this App, a secret key and token are provided to the App and from that point on, any file the merchant places in their Dropbox App can be accessed by their Shopify App.
Dealing with Large Amounts of Data Direct link to this section
It is one thing to have success and then another to manage it effectively. When there are 1000, 3000 or 10000 or more open orders in a shop it is beneficial to close them to keep the interface presenting the orders clean and manageable. Given a shop with a large number of orders to process, we download the ones that are open and paid, and we call that our haystack. It's a good idea to cache them during development since downloading takes a long time. The haystack is pretty much an array of order ID's and perhaps a name.
The next thing to do is download the merchant's CSV from the Dropbox containing the needles of interest. Typically a row from a CSV contains at least an order ID and a tracking number. Row by row a parse of this data builds up a new data structure called work which is an array of order ID's and the tracking number to associate with them. This work is all constructed in a delayed job since it can take a long time to construct it. This job stores the work in the cache using a key labelled work and before terminating, spawns a new job that will chew through the work.
Chewing Through Work and API Calls Direct link to this section
The second job spawned comes to life by opening the cache and looking for a key called work. If there is anything to be done, the process is simple. Using the Shopify API open the order specified in the work file. Create a new fulfillment for that order with the tracking number and the setting for whether or not to alert the customer using the Shopify Shipping Update email. Once that fulfillment is created, the order can then be closed. If during this cycle there are no more API calls remaining, the job spawns a new copy of itself to run in 5 minutes and it terminates. As long as each successful entry in the work array is removed upon completion, this cycle is perfect for chewing through thousands of API calls without worrying about the limits.
Summary Direct link to this section
A Shopify App to fulfill orders and add tracking numbers can be built using the Shopify API, Dropbox and Delayed Job. The merchant need only upload a CSV to Bropbox and initiate the updates with the press of a button.