← 返回列表
全部 Skill›商业 Business›e-commerce

e-commerce

作者 @YepAPI · 语言 en
Cart state with Zustand + persistence, product variants, Stripe checkout, inventory management, and order lifecycle.
商业 Business电子商务需海外环境访问网络待翻译
存储库
最近源活动
—
检测 SKILL.md 语言
en
星标 ★
11
分支 Fork
0
安装选项
提示
指令
使用 AI 助手安装
将下面的提示复制到你正在使用的 AI 助手中。
下载完整 Skill 目录(含全部配套文件)。 下载压缩包 ↓
SKILL.md 原文预览

E-Commerce

Rules

  • Cart state: Zustand store with localStorage persistence — survives page refresh and tab close
  • Product variants: model as separate SKUs with shared parent product — each variant has its own price, inventory, image
  • Checkout: Stripe Checkout Sessions (server-side) — never send prices from the client
  • Inventory: check stock server-side before creating checkout session — race condition protection with database transactions
  • Order lifecycle: pending → paid → shipped → delivered — update via webhook events, not client actions
  • Product listing: server-side filtering/sorting/pagination — URL search params for shareable filtered views
  • Prices: store in cents (integers) — display with Intl.NumberFormat and currency code
  • Cart operations: add, remove, update quantity, clear — validate stock on every cart action

Patterns

// Zustand cart store with persistence
import { create } from "zustand";
import { persist } from "zustand/middleware";

interface CartItem { productId: string; variantId: string; quantity: number; price: number; }
interface CartStore {
  items: CartItem[];
  addItem: (item: CartItem) => void;
  removeItem: (variantId: string) => void;
  updateQuantity: (variantId: string, quantity: number) => void;
  clear: () => void;
  total: () => number;
}

export const useCart = create<CartStore>()(
  persist(
    (set, get) => ({
      items: [],
      addItem: (item) => set((state) => {
        const existing = state.items.find((i) => i.variantId === item.variantId);
        if (existing) {
          return { items: state.items.map((i) =>
            i.variantId === item.variantId ? { ...i, quantity: i.quantity + item.quantity } : i
          )};
        }
        return { items: [...state.items, item] };
      }),
      removeItem: (variantId) => set((s) => ({ items: s.items.filter((i) => i.variantId !== variantId) })),
      updateQuantity: (variantId, quantity) => set((s) => ({
        items: quantity <= 0
          ? s.items.filter((i) => i.variantId !== variantId)
          : s.items.map((i) => (i.variantId === variantId ? { ...i, quantity } : i)),
      })),
      clear: () => set({ items: [] }),
      total: () => get().items.reduce((sum, i) => sum + i.price * i.quantity, 0),
    }),
    { name: "cart-storage" }
  )
);

Avoid

  • Sending prices from the client to create checkout — always resolve prices server-side from your database
  • Storing cart only in React state — use persisted Zustand or localStorage so cart survives refresh
  • Deducting inventory on "add to cart" — only reserve stock at checkout, release on session expiry
  • Updating order status from the client — use Stripe webhooks for checkout.session.completed
  • Floating point for money — store as integers (cents), format for display only
评论
YYepAPI
YepAPI
YepAPI/skills
查看 GitHub 仓库 查看作者的其他 Skill
来源信息
作者@YepAPI
所属大类商业 Business / 电子商务
国内可用性需海外环境
最近源活动—
风险提示访问网络
提示:风险标签为程序静态扫描结果,仅作提示,不代表安全认证。安装前请阅读 SKILL.md 及其引用的脚本、权限,自行判断是否可信。