Photo by Álvaro Serrano on Unsplash
“I’m a developer, not a God d*mn point & click machine.”
That’s my dev friend Nic. We’re talking e-commerce.
Few pints in and he cranks up the ranting:
“You know what I don’t like? Here:
If you want that Shopify site so bad, you can do it yourself, you don’t need ME (dev).”
I love Nic.
And he’s right: there’s a plethora of cool frameworks/tools out there. Why not create a custom, kick*ss shopping experience with these instead?
Today I want to use Vue.js to show you that’s possible. Because yeah, Vue.js e-commerce is a thing!
And with the right set up in the backend — headless content management, namely — it can be a darn cool thing.
Enough babbling, here’s what this Vue e-commerce example will include:
Now let’s make my friend Nic proud, shall we?
So, if you haven’t heard already: Vue is a progressive, lightweight JS framework for building web UIs.
It’s open source, community-supported, reactive, component-based. It’s also quite “incremental” — can be dropped in any existing projects — and works wonders for logic concerns separation.
So why use Vue for e-commerce, huh?
In this quick tuts, I’m going to abstract some of the cart & e-comm. logic by using Snipcart. But you could very well build your own Vue.js shopping cart & hook it to Stripe, for instance.
Oh, and another compelling use case for Vue e-commerce: progressive web applications (PWAs). If your shopping context requires graceful degradation and/or offline functionalities, building an e-comm. PWA with Vue becomes quite handy. On that note, you might want to check out Vue Storefront.
Sidenote: we’ll most likely be exploring PWAs in an upcoming blog post. :)
ButterCMS is headless CMS.
Or an API-first CMS, or a Content API, or whatever else the next marketing guy decides to call it. :)
Why use a headless CMS as an e-commerce backend, you ask?
Well, for starters, maybe you’re just tired of using behemoths like Magento or Prestashop.
Or maybe you want your online store to benefit from a headless CMS’ pros:
I don’t want to get too deep into headless e-commerce’s ins and outs, because we have a full post coming on that topic.
However, I’d like to highlight some of the good stuff ButterCMS brings to the table:
1. It has intuitive, thoroughly-documented content modeling.2. Its onboarding is smooth and effective.3. It has in-depth, stack-specific integration recipes.
The latter gave me a tremendous head start for this Vue tuts!
Pre-requisites
ButterCMS allows to easily create page types with any required fields. This can all be done without any code or schemas definition from the CMS.
Start by creating a Product page type with fields for every required attribute of Snipcart’s product definition (data-item-id, data-item-name, data-item-price). I didn’t define the product URL used for price validation: it will be generated later by code.
You can see that I’ve also added a description field. It’ll map to Snipcart’s data-item-description
. You could add more fields if you want!
You should now be able to create content pages of the Product type:
Following ButterCMS’ Vue guide, I’ve used vue-cli
to generate my e-commerce demo. But you could easily add Butter to an existing project.
Now, create a simple src/buttercms.js
file. Then initialize Butter's library with your Butter public API Key to import that file into your Vue.js components:
Use the routes below for your Vue app: a list of products, a page for the products’ details and a NotFound
component (to be re-used later).
Here comes the fun part.
To show products from Butter’s API, just call butter.page.list('product')
from the ProductList
component's created
hook. Then, the reactivity of Vue will work its magic!
Okay, that was easy. How about displaying the page for a single product? Just use the slug
parameter from your route!
You can see above that I’ve used the error handler of the Promise
to set a notFound
flag. In the component's template, this allows you to either display the product's details or the NotFound page without changing the URL:
Now for integrating Snipcart, create a simple BuyButton
component:
Because this e-commerce demo is a Single Page Application, I’ve added a Netlify Function to make Snipcart product validation work.
You could have used Webtask or any serverless tech. The function uses Butter API from Node.js to retrieve products and output them to be parsed by Snipcart’s JSON Crawler.
And there you have it: a Vue.js e-commerce website powered by ButterCMS.
You could push this a bit further, though. Butter offers webhooks that can be used to automate tasks.
You can use them to notify Snipcart of CMS updates on products so your e-comm. dashboard reflects the changes automatically. Quite simple, really:
Here:
Voilà.
As soon as you add or edit a product in the CMS, its details will be available in Snipcart’s Dashboard.
You could use that, for instance, to set an available stock for inventory management in your Dashboard.
Now let’s have a look at that ButterCMS + Vue.js e-commerce demo!
See live demo here
See GitHub repo here
Bundling this whole thing together and making it look decent took me two days tops.
Of course, I loved working with Vue for this e-commerce demo.
And Using ButterCMS was a smooth as its name suggest.
Constructive feedback: I would have liked to get an edition API — it could’ve allowed me, for instance, to sync a flag in the CMS for out-of-stock products.
Well, that’s it, folks!
Here’s hoping this post nudges you towards killer, custom e-commerce. :)
If you’ve enjoyed this post, please take a second to clap & share it on Twitter. Got comments, questions? Hit the section below!
I originally published this on the Snipcart blog and shared it in our newsletter.