-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: New page for Shopify #17793
Merged
Merged
docs: New page for Shopify #17793
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
38801bd
Added shopify ecommerce site
4624916
Better Shopify instructions
f5dd81a
Better heading
5422460
It's an imperative verb, not a noun
31124f9
Added shopify-buy
db671de
Cleanup, editing
kevee e5197d9
Merge branch 'master' of https://github.com/gatsbyjs/gatsby into docs…
kevee 881e6be
Apply suggestions from code review
kevee 7ca28d2
Apply suggestions from code review
kevee c1c695a
Merge remote-tracking branch 'upstream/master' into docs/shopify-10418
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
--- | ||
title: Building an e-commerce site with Shopify | ||
--- | ||
|
||
In this tutorial, you will setup a new Gatsby website that fetches product data from [Shopify](https://www.shopify.com). The site displays a list of all products on a product listing page, and a page for every product in the store. | ||
|
||
If you are already comfortable with Gatsby and Shopify, you might want to check out the [Gatsby Shopify starter](https://www.gatsbyjs.org/starters/AlexanderProd/gatsby-shopify-starter/), which provides many of the same features as this example. | ||
|
||
## Setting up your Shopify account | ||
|
||
1. Create a new [Shopify account](https://www.shopify.com) and store if you don't have one. | ||
2. Create a private app in your store by navigating to `Apps`, then `Manage private apps`. | ||
3. Create a new private app, with any "Private app name" and leaving the default permissions as Read access under Admin API. | ||
4. Enable the [Shopify Storefront API](https://help.shopify.com/en/api/storefront-api) by checking the box that says "Allow this app to access your storefront data using Storefront API". Make sure to also grant access to read product and customer tags by checking their corresponding boxes. | ||
|
||
## Set up the Gatsby Shopify plugin | ||
|
||
1. If you do not already have one ready, [create a Gatsby site](https://www.gatsbyjs.org/docs/quick-start). | ||
|
||
2. Install the [`gatsby-source-shopify`](/packages/gatsby-source-shopify/) plugin and [`shopify-buy`](https://github.com/Shopify/js-buy-sdk) package. | ||
|
||
```shell | ||
npm install --save gatsby-source-shopify shopify-buy | ||
``` | ||
|
||
3. Enable and configure the plugin in your `gatsby-config.js` file, replacing [some-shop] with your shop name and [token] with your Storefront access token. | ||
|
||
```javascript:title=/gatsby-config.js | ||
plugins: [ | ||
{ | ||
resolve: `gatsby-source-shopify`, | ||
options: { | ||
// The domain name of your Shopify shop. | ||
shopName: `[some-shop]`, | ||
|
||
// The storefront access token | ||
accessToken: `[token]`, | ||
}, | ||
}, | ||
] | ||
``` | ||
|
||
4. Run `gatsby develop` and make sure the site compiles successfully. | ||
|
||
## Querying Shopify data and listing products | ||
|
||
Open the Gatsby GraphiQL interface by visiting `http://localhost:8000/___graphql`. With at least one example product added into Shopify you should see several new types of nodes in the Explorer tab, like `allShopifyProduct`. To query all products in your store sorted by title, try running the query: | ||
|
||
```graphql | ||
{ | ||
allShopifyProduct(sort: { fields: [title] }) { | ||
edges { | ||
node { | ||
title | ||
images { | ||
originalSrc | ||
} | ||
shopifyId | ||
description | ||
availableForSale | ||
priceRange { | ||
maxVariantPrice { | ||
amount | ||
} | ||
minVariantPrice { | ||
amount | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
To add a simple page listing all products, add a new file at `/src/pages/products.js`. | ||
gillkyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```jsx:title=/src/pages/products.js | ||
import React from "react" | ||
import { Link, graphql } from "gatsby" | ||
|
||
import Layout from "../components/layout" | ||
|
||
const ProductsPage = ({ data }) => ( | ||
<Layout> | ||
<h1>Products</h1> | ||
<ul> | ||
{data.allShopifyProduct.edges.map(({ node }) => ( | ||
<li key={node.shopifyId}> | ||
<h3> | ||
<Link to={`/product/${node.handle}`}>{node.title}</Link> | ||
{" - "}${node.priceRange.minVariantPrice.amount} | ||
</h3> | ||
<p>{node.description}</p> | ||
</li> | ||
))} | ||
</ul> | ||
</Layout> | ||
) | ||
|
||
export default ProductsPage | ||
|
||
export const query = graphql` | ||
{ | ||
allShopifyProduct(sort: { fields: [title] }) { | ||
edges { | ||
node { | ||
title | ||
shopifyId | ||
description | ||
handle | ||
priceRange { | ||
minVariantPrice { | ||
amount | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
``` | ||
|
||
## Generating a page for each product | ||
|
||
You can [programatically create pages](/tutorial/part-seven/) in Gatsby for every product in your Shopify store. | ||
|
||
Create a template for your product pages by adding a new file, `/src/templates/product.js`. | ||
gillkyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```jsx:title=/src/templates/product.js | ||
import React from "react" | ||
|
||
import Layout from "../components/layout" | ||
|
||
const ProductTemplate = ({ pageContext }) => { | ||
const { product } = pageContext | ||
return ( | ||
<Layout> | ||
<h1>{product.title}</h1> | ||
<div>{product.description}</div> | ||
</Layout> | ||
) | ||
} | ||
|
||
export default ProductTemplate | ||
``` | ||
|
||
Edit your `gatsby-node.js` file and add the following code. This queries all Shopify products with GraphQL, then creates a page using the template in `product.js`. Each page gets assigned a URL in the format of `/products/[product handle]`. | ||
|
||
```javascript:title=/gatsby-node.js | ||
const path = require(`path`) | ||
exports.createPages = async ({ graphql, actions }) => { | ||
const { createPage } = actions | ||
// Query for all products in Shopify | ||
const result = await graphql(` | ||
query { | ||
allShopifyProduct(sort: { fields: [title] }) { | ||
edges { | ||
node { | ||
title | ||
images { | ||
originalSrc | ||
} | ||
shopifyId | ||
handle | ||
description | ||
availableForSale | ||
priceRange { | ||
maxVariantPrice { | ||
amount | ||
} | ||
minVariantPrice { | ||
amount | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`) | ||
// Iterate over all products and create a new page using a template | ||
// The product "handle" is generated automatically by Shopify | ||
result.data.allShopifyProduct.edges.forEach(({ node }) => { | ||
createPage({ | ||
path: `/product/${node.handle}`, | ||
component: path.resolve(`./src/templates/product.js`), | ||
context: { | ||
product: node, | ||
}, | ||
}) | ||
}) | ||
} | ||
``` | ||
|
||
## Additional Resources | ||
|
||
- [Gatsby Shopify Starter](/starters/AlexanderProd/gatsby-shopify-starter/) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also add here a notice about private accestoken And link to the environment variable page?