How to do Fulfillments

Aug 27, 2013

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

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

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

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

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.

For accounting purposes there are a couple of ways of dealing with selling and purchasing of inventory and the changes in the cost of purchasing inventory over time. It is rare that a merchant will purchase items to sell in their Shopify store for the exact same price over time. It can be important to use an average cost price that takes into account these purchasing price changes over time.

The Profiteer App is meant to expose profit or margins on a sale by subtracting the cost price of goods sold from the selling price of the goods sold. A common business issue faced by many merchants is the effect currency exchange rates may have on their sales. A shop located in Canada may sell products to Canadians in Canadian currency, but the products sold may have been purchased and imported from the United States. Many merchants probably source their products from the United States, and it is considered to be one of the standard currencies by which most others are measured.

Had a real laugher/shocker at the same time. I develop so much App code where the data processing is a flow and not a stop:persist:process:continue action that sometimes you get burned by that. Everything was working fine as “hey download all that shop’s orders, parse them, deal with the contents, and move on”. Everything as working fine on MY itty, bitty, little dev shop.

As a way of welcoming in the new year of 2014 and trying to decide which way was up from down, left from right, I made a decision to be a speaker at the local Montréal Ruby group. Having attended a dozen or more meetings over the years I felt like I had 20 minutes of quality material without being too technical, or wildly off of one topic.