We’ve been using Shopify at ThankLab and chose them because they’re a hosted solution and have a simple API. However, we quickly uncovered many limitations that required us to come up with workarounds. Some of the issues we had to resolve were a result of integrating Shopify into a third-party website with existing user accounts.
As we dug into documentation on the official Shopify wiki and blog, it became clear that many of the solutions they provide are their own hacks around limitations many customers have run into. This realization was double-edged: Shopify clearly encourages hacking on their platform, but many capabilities or “features” are not really baked into their core product. In the end (frustrations aside), the challenge of making Shopify work for us became an exercise in creativity.
Seamlessly integrating third-party user accounts with Shopify checkout
Our users login to our website and checkout on Shopify. Shopify has these 3 options at checkout:
- Guest checkout – requires the customer to enter their email address
- Customer login only – requires email and password to login, however each customer needs to be manually invited via email to create passwords for their account
Goal: pre-fill email address on guest checkout page (and hide email field)
Shopify limitations: Checkout page has a unique URL (is not known ahead of time), does not accept query string parameters to pre-fill form fields, and only allows you to customize CSS.
Get the Shopify checkout URL ahead of time
I kinda lied there a little bit. The checkout page CAN be determined ahead of time, but because the URL has a random looking token in it, it isn’t immediately evident. Here’s an example of my cart on the Penny Arcade store:
- Get your shop ID from your checkout page URL (simply click “checkout” in your shop)
var shop_id = "429942";
var checkout_url = "https://checkout.shopify.com/carts/" + shop_id + "/" + readCookie("cart");
We will use this URL to add some parameters to pre-fill the checkout form. Download my example script (shopify_hacks.js) from Github.
Pre-filling the Shopify checkout form
We are now able to determine the Shopify checkout URL ahead of time, but need to pre-fill the form. If you’re familiar with Ruby on Rails or web development frameworks in general, you know that you can typically take the names of form fields and add them to the URL as query string values. Those values are then pre-populated in forms that have fields matching those parameters.
In the case of the checkout form, if you view the HTML source of the page in your browser and find the “input” tags, you’ll find the field for the email address on guest checkout. The name is “order[email]“. Now, when you add that name as a query string value, the checkout URL looks like this:
Unfortunately this didn’t work. The email address is still blank. However, if you submit the form with invalid information (e.g. blank email address), you’ll notice that the cart URL changes. The URL now ends in “create_order”:
Now you can see that my email address is pre-populated in the form. You can pre-populate all form fields if you wish. In our case we are only pre-populating the email field and using CSS to hide the table row. This effectively lets our users checkout without re-entering the email address we already know.
Alternatively, use Mechanize to automate creating Shopify customer accounts with passwords
Goal: Create Shopify customer logins linked to third-party user database
Shopify limitation: Shopify customer logins can only be created by inviting a customer (manually) via email, at which point customers can create a password.
Originally wanted to create a Shopify customer account for each user in our third-party database, but Shopify doesn’t let you create customer accounts through the API. For some reason the only way to create a Shopify customer account is to manually “invite” the user via email, which sends them a link to create a password. We ended up automating this process using Mechanize:
- Create a Shopify customer using the API
- Create a Shopify administrator account that only has access to Customers
- Mechanize logs in to shop admin with administrator account
- Mechanize retrieves the invite link for a customer
- Mechanize visits the invite link and fills out the form to create a password
Pretty simple. However, we felt that this was less reliable than the guest checkout method. This process would also sometimes take up to 7 seconds to complete, and we decided it would add more complexity to our application to deal with this latency, as well as making sure that our users would be logged in to their Shopify accounts behind the scenes reliably (read: posting their Shopify username and password to the login form on Shopify in a hidden iframe).
Download the mechanize script (mechanize_activate_customer_login.rb) from Github. It’s a Ruby script that you can run from the command line. It accepts several arguments and outputs how long each step takes. Make sure to install the Mechanize gem before running the script.
Some other convenient undocumented Shopify functionality discovered
We actually have our own custom cart on a website separate from Shopify. So, when users click the “checkout” button, we need to “copy” the items we have in our cart to the Shopify cart. To do this, we first need to clear their existing Shopify cart and then post all items to Shopify.
Redirect after clearing Shopify cart
To clear a Shopify cart, you visit the “/cart/clear” page. After the cart is cleared, it automatically redirects you back to the empty cart page:
However, if you want to send your user to a page of your choosing, all you have to do is add that page to the URL as a “return_to” parameter:
This parameter is documented for adding products to the cart, but not any other pages. Presumably, you can add the “return_to” parameter to more pages that perform actions within Shopify.