|
| 1 | +# Shopify Integration Setup Guide |
| 2 | + |
| 3 | +This guide will help you configure the Shopify integration for your Vets Who Code application. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Shopify integration allows you to: |
| 8 | +- Display products from your Shopify store on your Next.js website |
| 9 | +- Provide a full shopping cart experience |
| 10 | +- Handle checkout through Shopify's secure checkout process |
| 11 | +- Manage products, inventory, and orders through your Shopify admin |
| 12 | + |
| 13 | +## What's Been Implemented |
| 14 | + |
| 15 | +### 1. Backend Infrastructure |
| 16 | +- **Shopify Client Library** (`/src/lib/shopify.ts`) |
| 17 | + - GraphQL queries for products, collections, and cart |
| 18 | + - Type-safe TypeScript interfaces |
| 19 | + - Helper functions for price formatting |
| 20 | + |
| 21 | +- **API Routes** (`/src/pages/api/shopify/`) |
| 22 | + - `GET /api/shopify/products` - Get all products |
| 23 | + - `GET /api/shopify/products/[handle]` - Get single product |
| 24 | + - `GET /api/shopify/collections` - Get all collections |
| 25 | + - `GET /api/shopify/collections/[handle]` - Get single collection |
| 26 | + - `POST /api/shopify/cart/create` - Create new cart |
| 27 | + - `GET /api/shopify/cart/[cartId]` - Get cart details |
| 28 | + - `POST /api/shopify/cart/add` - Add items to cart |
| 29 | + - `POST /api/shopify/cart/update` - Update cart quantities |
| 30 | + - `POST /api/shopify/cart/remove` - Remove items from cart |
| 31 | + |
| 32 | +### 2. Frontend Components |
| 33 | +- **ProductCard** (`/src/components/product-card/`) - Individual product display with "Add to Cart" |
| 34 | +- **ProductGrid** (`/src/components/product-grid/`) - Responsive grid layout for products |
| 35 | +- **ShoppingCart** (`/src/components/shopping-cart/`) - Slide-out cart drawer with quantity controls |
| 36 | +- **useCart Hook** (`/src/hooks/useCart.ts`) - Cart state management with localStorage persistence |
| 37 | + |
| 38 | +### 3. Pages |
| 39 | +- **Store Page** (`/src/pages/store/index.tsx`) - Main storefront with all products |
| 40 | +- **Product Detail Page** (`/src/pages/store/products/[handle].tsx`) - Individual product page with variant selection |
| 41 | + |
| 42 | +## Setup Instructions |
| 43 | + |
| 44 | +### Step 1: Create Shopify Storefront API Access Token |
| 45 | + |
| 46 | +1. Log in to your Shopify admin panel |
| 47 | +2. Go to **Settings** → **Apps and sales channels** → **Develop apps** |
| 48 | +3. Click **Create an app** (if you haven't already) |
| 49 | +4. Name it something like "Vets Who Code Website" |
| 50 | +5. Click on **Configure Storefront API scopes** |
| 51 | +6. Enable the following scopes: |
| 52 | + - `unauthenticated_read_product_listings` |
| 53 | + - `unauthenticated_read_product_inventory` |
| 54 | + - `unauthenticated_read_product_tags` |
| 55 | + - `unauthenticated_write_checkouts` |
| 56 | + - `unauthenticated_read_checkouts` |
| 57 | + - `unauthenticated_write_customers` |
| 58 | +7. Click **Save** |
| 59 | +8. Go to **API credentials** tab |
| 60 | +9. Click **Install app** |
| 61 | +10. Copy your **Storefront API access token** |
| 62 | + |
| 63 | +### Step 2: Configure Environment Variables |
| 64 | + |
| 65 | +Add these variables to your `.env` or `.env.local` file: |
| 66 | + |
| 67 | +```bash |
| 68 | +# Shopify Configuration |
| 69 | +SHOPIFY_STORE_DOMAIN="your-store.myshopify.com" |
| 70 | +SHOPIFY_STOREFRONT_ACCESS_TOKEN="your-storefront-access-token" |
| 71 | +SHOPIFY_ADMIN_ACCESS_TOKEN="your-admin-access-token" # Optional for admin features |
| 72 | +``` |
| 73 | + |
| 74 | +**Important Notes:** |
| 75 | +- Replace `your-store` with your actual Shopify store name |
| 76 | +- The domain should include `.myshopify.com` |
| 77 | +- Never commit your `.env` file to version control |
| 78 | +- The `.env.example` file has been updated with these variables |
| 79 | + |
| 80 | +### Step 3: Test the Integration |
| 81 | + |
| 82 | +1. **Start your development server:** |
| 83 | + ```bash |
| 84 | + npm run dev |
| 85 | + ``` |
| 86 | + |
| 87 | +2. **Visit the store page:** |
| 88 | + - Navigate to `http://localhost:3000/store` |
| 89 | + - You should see your products displayed |
| 90 | + |
| 91 | +3. **Test the cart:** |
| 92 | + - Click "Add to Cart" on any product |
| 93 | + - Click the cart icon to view your cart |
| 94 | + - Try updating quantities or removing items |
| 95 | + - Click "Proceed to Checkout" to test Shopify checkout |
| 96 | + |
| 97 | +### Step 4: Deploy to Production |
| 98 | + |
| 99 | +1. **Add environment variables to your hosting platform** (Vercel, Netlify, etc.) |
| 100 | + - Add the same `SHOPIFY_STORE_DOMAIN` and `SHOPIFY_STOREFRONT_ACCESS_TOKEN` |
| 101 | + |
| 102 | +2. **Build and deploy:** |
| 103 | + ```bash |
| 104 | + npm run build |
| 105 | + npm start |
| 106 | + ``` |
| 107 | + |
| 108 | +3. **Verify ISR (Incremental Static Regeneration):** |
| 109 | + - Products will be cached and revalidated every 5 minutes |
| 110 | + - This provides fast page loads with fresh data |
| 111 | + |
| 112 | +## Usage |
| 113 | + |
| 114 | +### Adding Products to Your Store |
| 115 | + |
| 116 | +1. Add products in your Shopify admin at `https://[your-store].myshopify.com/admin/products` |
| 117 | +2. Products will automatically appear on your website within 5 minutes (or immediately in development) |
| 118 | +3. Make sure products are: |
| 119 | + - Published to the "Online Store" sales channel |
| 120 | + - Have at least one image |
| 121 | + - Have a valid price |
| 122 | + - Are in stock |
| 123 | + |
| 124 | +### Cart Behavior |
| 125 | + |
| 126 | +- Cart data is stored in the user's browser using localStorage |
| 127 | +- Cart persists across page refreshes |
| 128 | +- Cart ID is managed automatically |
| 129 | +- Users are redirected to Shopify's secure checkout when they click "Proceed to Checkout" |
| 130 | + |
| 131 | +### Customization |
| 132 | + |
| 133 | +#### Changing Product Grid Layout |
| 134 | +Edit `/src/pages/store/index.tsx`: |
| 135 | +```tsx |
| 136 | +<ProductGrid products={products} columns={4} /> // 2, 3, or 4 columns |
| 137 | +``` |
| 138 | + |
| 139 | +#### Styling |
| 140 | +All components use TailwindCSS with the `tw-` prefix. Modify classes in: |
| 141 | +- `/src/components/product-card/product-card.tsx` |
| 142 | +- `/src/components/shopping-cart/shopping-cart.tsx` |
| 143 | +- `/src/pages/store/index.tsx` |
| 144 | + |
| 145 | +#### Adding Collections |
| 146 | +To add collection pages: |
| 147 | +1. Create `/src/pages/store/collections/[handle].tsx` |
| 148 | +2. Use the existing collection API routes |
| 149 | +3. Follow the pattern from the product detail page |
| 150 | + |
| 151 | +## Troubleshooting |
| 152 | + |
| 153 | +### "Store Configuration Required" Message |
| 154 | +- Check that your `.env` file contains the correct Shopify credentials |
| 155 | +- Restart your development server after adding environment variables |
| 156 | +- Verify the Storefront API access token is valid |
| 157 | + |
| 158 | +### Products Not Showing |
| 159 | +- Ensure products are published to the "Online Store" sales channel in Shopify |
| 160 | +- Check that products have inventory available |
| 161 | +- Look for errors in the browser console or server logs |
| 162 | + |
| 163 | +### Cart Not Working |
| 164 | +- Check browser console for JavaScript errors |
| 165 | +- Verify localStorage is enabled in the browser |
| 166 | +- Clear localStorage and try again: `localStorage.clear()` |
| 167 | + |
| 168 | +### Checkout Redirect Issues |
| 169 | +- Ensure your Shopify store's checkout settings are configured |
| 170 | +- Verify the cart has valid items |
| 171 | +- Check that the `checkoutUrl` is being returned from the API |
| 172 | + |
| 173 | +## API Reference |
| 174 | + |
| 175 | +### Shopify Client Functions |
| 176 | + |
| 177 | +```typescript |
| 178 | +import { |
| 179 | + getProducts, |
| 180 | + getProduct, |
| 181 | + getCollections, |
| 182 | + getCollection, |
| 183 | + createCart, |
| 184 | + getCart, |
| 185 | + addToCart, |
| 186 | + updateCartLines, |
| 187 | + removeFromCart, |
| 188 | + formatPrice, |
| 189 | + isShopifyConfigured, |
| 190 | +} from '@lib/shopify'; |
| 191 | +``` |
| 192 | + |
| 193 | +### useCart Hook |
| 194 | + |
| 195 | +```typescript |
| 196 | +const { |
| 197 | + cart, // Current cart object |
| 198 | + cartCount, // Total number of items |
| 199 | + cartTotal, // Total price amount |
| 200 | + cartCurrency, // Currency code |
| 201 | + isLoading, // Loading state |
| 202 | + error, // Error message |
| 203 | + addToCart, // Add items to cart |
| 204 | + updateCartLines, // Update quantities |
| 205 | + removeFromCart, // Remove items |
| 206 | + clearCart, // Clear entire cart |
| 207 | + refreshCart, // Refresh cart data |
| 208 | +} = useCart(); |
| 209 | +``` |
| 210 | + |
| 211 | +## Next Steps |
| 212 | + |
| 213 | +### Optional Enhancements |
| 214 | + |
| 215 | +1. **Add Collections Pages** |
| 216 | + - Create collection listing pages |
| 217 | + - Filter products by collection |
| 218 | + - Add collection navigation |
| 219 | + |
| 220 | +2. **Product Search** |
| 221 | + - Implement search functionality |
| 222 | + - Use Shopify's search query syntax |
| 223 | + - Add autocomplete |
| 224 | + |
| 225 | +3. **Wishlist Feature** |
| 226 | + - Save products for later |
| 227 | + - Use localStorage or database |
| 228 | + - Share wishlists |
| 229 | + |
| 230 | +4. **Product Reviews** |
| 231 | + - Integrate review app from Shopify |
| 232 | + - Display reviews on product pages |
| 233 | + - Add review submission |
| 234 | + |
| 235 | +5. **Discount Codes** |
| 236 | + - Apply discount codes in cart |
| 237 | + - Display discount amounts |
| 238 | + - Use Shopify Checkout API |
| 239 | + |
| 240 | +6. **Related Products** |
| 241 | + - Show related/recommended products |
| 242 | + - Use product tags or AI |
| 243 | + - Increase cross-sells |
| 244 | + |
| 245 | +## Support |
| 246 | + |
| 247 | +For issues with: |
| 248 | +- **Shopify API**: Visit [Shopify Developer Docs](https://shopify.dev/docs/api/storefront) |
| 249 | +- **Next.js**: Visit [Next.js Docs](https://nextjs.org/docs) |
| 250 | +- **Integration Issues**: Check the browser console and server logs |
| 251 | + |
| 252 | +## Resources |
| 253 | + |
| 254 | +- [Shopify Storefront API Documentation](https://shopify.dev/docs/api/storefront) |
| 255 | +- [Shopify GraphQL Explorer](https://shopify.dev/docs/api/storefront/2024-01/queries) |
| 256 | +- [Next.js Static Site Generation](https://nextjs.org/docs/basic-features/data-fetching/get-static-props) |
| 257 | +- [TypeScript Shopify Types](https://github.com/Shopify/hydrogen/tree/main/packages/hydrogen-react) |
| 258 | + |
| 259 | +--- |
| 260 | + |
| 261 | +**Created**: November 2024 |
| 262 | +**Integration Version**: Shopify Storefront API 2024-01 |
| 263 | +**Framework**: Next.js 15 with TypeScript |
0 commit comments