This guide will help you configure the Shopify integration for your Vets Who Code application.
The Shopify integration allows you to:
- Display products from your Shopify store on your Next.js website
- Provide a full shopping cart experience
- Handle checkout through Shopify's secure checkout process
- Manage products, inventory, and orders through your Shopify admin
-
Shopify Client Library (
/src/lib/shopify.ts)- GraphQL queries for products, collections, and cart
- Type-safe TypeScript interfaces
- Helper functions for price formatting
-
API Routes (
/src/pages/api/shopify/)GET /api/shopify/products- Get all productsGET /api/shopify/products/[handle]- Get single productGET /api/shopify/collections- Get all collectionsGET /api/shopify/collections/[handle]- Get single collectionPOST /api/shopify/cart/create- Create new cartGET /api/shopify/cart/[cartId]- Get cart detailsPOST /api/shopify/cart/add- Add items to cartPOST /api/shopify/cart/update- Update cart quantitiesPOST /api/shopify/cart/remove- Remove items from cart
- ProductCard (
/src/components/product-card/) - Individual product display with "Add to Cart" - ProductGrid (
/src/components/product-grid/) - Responsive grid layout for products - ShoppingCart (
/src/components/shopping-cart/) - Slide-out cart drawer with quantity controls - useCart Hook (
/src/hooks/useCart.ts) - Cart state management with localStorage persistence
- Store Page (
/src/pages/store/index.tsx) - Main storefront with all products - Product Detail Page (
/src/pages/store/products/[handle].tsx) - Individual product page with variant selection
- Log in to your Shopify admin panel
- Go to Settings → Apps and sales channels → Develop apps
- Click Create an app (if you haven't already)
- Name it something like "Vets Who Code Website"
- Click on Configure Storefront API scopes
- Enable the following scopes:
unauthenticated_read_product_listingsunauthenticated_read_product_inventoryunauthenticated_read_product_tagsunauthenticated_write_checkoutsunauthenticated_read_checkoutsunauthenticated_write_customers
- Click Save
- Go to API credentials tab
- Click Install app
- Copy your Storefront API access token
Add these variables to your .env or .env.local file:
# Shopify Configuration
SHOPIFY_STORE_DOMAIN="your-store.myshopify.com"
SHOPIFY_STOREFRONT_ACCESS_TOKEN="your-storefront-access-token"Important Notes:
- Replace
your-storewith your actual Shopify store name - The domain should include
.myshopify.com - Never commit your
.envfile to version control - The
.env.examplefile has been updated with these variables
-
Start your development server:
npm run dev
-
Visit the store page:
- Navigate to
http://localhost:3000/store - You should see your products displayed
- Navigate to
-
Test the cart:
- Click "Add to Cart" on any product
- Click the cart icon to view your cart
- Try updating quantities or removing items
- Click "Proceed to Checkout" to test Shopify checkout
-
Add environment variables to your hosting platform (Vercel, Netlify, etc.)
- Add the same
SHOPIFY_STORE_DOMAINandSHOPIFY_STOREFRONT_ACCESS_TOKEN
- Add the same
-
Build and deploy:
npm run build npm start
-
Verify ISR (Incremental Static Regeneration):
- Products will be cached and revalidated every 5 minutes
- This provides fast page loads with fresh data
- Add products in your Shopify admin at
https://[your-store].myshopify.com/admin/products - Products will automatically appear on your website within 5 minutes (or immediately in development)
- Make sure products are:
- Published to the "Online Store" sales channel
- Have at least one image
- Have a valid price
- Are in stock
- Cart data is stored in the user's browser using localStorage
- Cart persists across page refreshes
- Cart ID is managed automatically
- Users are redirected to Shopify's secure checkout when they click "Proceed to Checkout"
Edit /src/pages/store/index.tsx:
<ProductGrid products={products} columns={4} /> // 2, 3, or 4 columnsAll components use TailwindCSS with the tw- prefix. Modify classes in:
/src/components/product-card/product-card.tsx/src/components/shopping-cart/shopping-cart.tsx/src/pages/store/index.tsx
To add collection pages:
- Create
/src/pages/store/collections/[handle].tsx - Use the existing collection API routes
- Follow the pattern from the product detail page
- Check that your
.envfile contains the correct Shopify credentials - Restart your development server after adding environment variables
- Verify the Storefront API access token is valid
- Ensure products are published to the "Online Store" sales channel in Shopify
- Check that products have inventory available
- Look for errors in the browser console or server logs
- Check browser console for JavaScript errors
- Verify localStorage is enabled in the browser
- Clear localStorage and try again:
localStorage.clear()
- Ensure your Shopify store's checkout settings are configured
- Verify the cart has valid items
- Check that the
checkoutUrlis being returned from the API
import {
getProducts,
getProduct,
getCollections,
getCollection,
createCart,
getCart,
addToCart,
updateCartLines,
removeFromCart,
formatPrice,
isShopifyConfigured,
} from '@lib/shopify';const {
cart, // Current cart object
cartCount, // Total number of items
cartTotal, // Total price amount
cartCurrency, // Currency code
isLoading, // Loading state
error, // Error message
addToCart, // Add items to cart
updateCartLines, // Update quantities
removeFromCart, // Remove items
clearCart, // Clear entire cart
refreshCart, // Refresh cart data
} = useCart();-
Add Collections Pages
- Create collection listing pages
- Filter products by collection
- Add collection navigation
-
Product Search
- Implement search functionality
- Use Shopify's search query syntax
- Add autocomplete
-
Wishlist Feature
- Save products for later
- Use localStorage or database
- Share wishlists
-
Product Reviews
- Integrate review app from Shopify
- Display reviews on product pages
- Add review submission
-
Discount Codes
- Apply discount codes in cart
- Display discount amounts
- Use Shopify Checkout API
-
Related Products
- Show related/recommended products
- Use product tags or AI
- Increase cross-sells
For issues with:
- Shopify API: Visit Shopify Developer Docs
- Next.js: Visit Next.js Docs
- Integration Issues: Check the browser console and server logs
- Shopify Storefront API Documentation
- Shopify GraphQL Explorer
- Next.js Static Site Generation
- TypeScript Shopify Types
Created: November 2024 Integration Version: Shopify Storefront API 2026-10 Framework: Next.js 15 with TypeScript
