.../articles/
Adding E-Commerce to Your Site with the Shopify Storefront API

Adding E-Commerce to Your Site with the Shopify Storefront API

2021.09.02

On projects we've worked on, we often get asked to add EC functionality to a site. Reference examples: ・ Himitsu Kitchen https://himitsu.kitchen/ ・ Julie - Find your one-of-a-kind bottle of wine from around the world https://julie.page/

In these cases, we often recommend using Shopify, and here we'll show just how easily you can add cart functionality for EC.

To add cart functionality, we use Shopify's Storefront API. The benefit of the Storefront API is that, rather than migrating your site onto the Shopify platform, you can add cart functionality just by adding JavaScript code to your existing codebase—which keeps development costs down. It also gives you freedom in your site architecture and lets you keep using the template engine or framework you're already familiar with. If you build directly on Shopify, you need to use a template language called Liquid, which can be a hurdle if you're not used to it.

Shopify Storefront API documentation

The Storefront API provides SDKs for JS, iOS, and Android, so you can easily work with product data and carts managed on Shopify just by using them.

Since this article is about websites, we'll walk through the approach using the JS SDK.

Installing the required package

$yarn add shopify-buy

Usage

Creating a cart

const fetch = require('node-fetch')
const shopify = require('shopify-buy')

// Create a checkout (i.e., a cart)

const shopifyClient = shopify.buildClient(shopifyConfig, fetch)
// Doc : https://github.com/Shopify/js-buy-sdk#creating-a-checkout
const checkout = await shopifyClient.checkout.create()

// Get the checkout token from the checkout you just created
const checkoutToken = checkout.id
// You can also retrieve an already-created checkout using a checkout token created previously
const checkout = await shopifyClient.checkout.fetch(checkoutToken)

Adding arbitrary information to the cart

You can add arbitrary information to a cart. Shopify carts provide CustomAttributes, which let you attach arbitrary information. That information can be viewed on Shopify's order screen. Use cases include adding a nickname used within your service, or adding a preferred delivery date.

// Doc : https://github.com/Shopify/js-buy-sdk#fetching-a-checkout
const shopifyClient = shopify.buildClient(shopifyConfig, fetch)
const userName = 'Information you want to save to the cart, such as the nickname used on the site'
const input = { customAttributes: { key: 'userName', value: `${userName}` } }
await shopifyClient.checkout.updateAttributes(checkoutToken, input)

Adding a product to the cart

const shopifyVariantId = '1234567891011'
// * This is the ID attached to the Shopify product information
// Note: this is different from the Product ID shown in the Shopify product screen's URL

const variantId = btoa(`gid://shopify/ProductVariant/${shopifyVariantId}`)
// Create the data to add to the cart
// You can also add information via CustomAttributes at the line item level
// Here, we add what will be displayed on the cart screen (the product's image and price)
const lineItemsObject = {
  variantId,
  quantity: 1,
  customAttributes: [
    { key: 'price', value: `${payload.price}` },
    { key: 'imageUrl', value: `${payload.coverImageUrl}` },
  ],
}
const shopifyClient = shopify.buildClient(shopifyConfig, fetch)
const checkout = await shopifyClient.checkout.addLineItems(checkoutToken, lineItemsObject)

Moving to the checkout page

You can navigate to checkout by running the following code when, for example, a checkout button is clicked.

const checkout = await shopifyClient.checkout.fetch(checkoutToken)
location.href = checkout.webUrl

Summary

This is how we were able to add cart functionality using the SDK. All that's left is to turn each necessary feature into a function and call it from the UI. If there's interest, we'd be happy to share a more detailed implementation.

written by

.../article/

Articles

All articles

From Firebase to Vercel, Contentful to microCMS — a migration log written with Claude Code

From Firebase to Vercel, Contentful to microCMS — a migration log written with Claude Code

We moved our corporate site's hosting and CMS, and made it bilingual along the way. The constraints we only found by running against real data were more useful than the migration itself, so this post focuses on where we got stuck.

Can't Read POST Data with Firebase Functions × Remix?

Can't Read POST Data with Firebase Functions × Remix?

How to read POST data from a Remix action when running on Firebase Functions.

Generative AI for Executives and Leaders: An Approach to Self-Driven DX

Generative AI for Executives and Leaders: An Approach to Self-Driven DX

Building a structure where executives and leaders themselves can identify issues and evaluate solutions using generative AI. We introduce how combining this with our hands-on support dramatically improves both the quality and speed of digital transformation.

Deploying a Monorepo Next.js App (App Router) to AWS Amplify

Deploying a Monorepo Next.js App (App Router) to AWS Amplify

Notes on the obstacles we hit while deploying a Next.js app managed in a monorepo to AWS Amplify.

Keeping Production Running Smoothly with Remote Work and Online Meetings [Documentation]

Keeping Production Running Smoothly with Remote Work and Online Meetings [Documentation]

Many production companies have adopted remote work as a result of the pandemic, and we are one of them.

Designing an E-Commerce Site That Sells: How to Find Great Reference Examples

Designing an E-Commerce Site That Sells: How to Find Great Reference Examples

There is no single formula for e-commerce design that sells. Driving revenue requires a solid concept, and getting to that concept requires thorough research.

Productivity Tools We Recommend as a Production Company, Including Services That Work Well Solo

Productivity Tools We Recommend as a Production Company, Including Services That Work Well Solo

With remote work becoming the norm during the COVID-19 pandemic, our team now works from home most days of the week.

We Released Thought Recorder, a Figma Plugin for Keeping a Commit History of Your Designs

We Released Thought Recorder, a Figma Plugin for Keeping a Commit History of Your Designs

We hope this helps web designers who work in Figma. Read on for how to use it.

How to Build an E-Commerce Site, and Which Platforms We Recommend

How to Build an E-Commerce Site, and Which Platforms We Recommend

Shopping online for fashion, appliances, and even groceries is now routine. With the pandemic accelerating the shift, we receive a steady stream of questions about which platform to use and how much it costs.

Generating FastAPI Schema Classes from OpenAPI

Generating FastAPI Schema Classes from OpenAPI

We chose FastAPI, a relatively modern framework, for a Python API project. FastAPI can generate an OpenAPI definition from your backend code, but here we do the opposite: generating FastAPI schema classes from an OpenAPI definition prepared in advance.

View all articles

Contact us