案例作品集 浏览精选项目

WESWOO 提供 Shopify Plus 项目开发费抵扣,至多 US$4,800

开发知识库 / 商品详情与组合购买

商品详情与组合购买

先同步型号与变体事实,再处理媒体、配件和规格信息。

型号、变体与购买表单

由选中的真实变体决定价格、可售状态和加购 ID。先提供可刷新的变体地址,再增强无刷新选择;图片、SKU 和网址都随同一状态更新。

数据与依赖

  • product 和 selected_or_first_available_variant。
  • 提交的是 variant.id,不是 product.id。

交互逻辑

  • GET 选择先刷新当前变体,再使用购买表单。
  • 缺货仍可查看,购买按钮按实际状态禁用。
示例代码 · snippets/ww-variant-fallback.liquid
Liquid · WESWOO
{% assign current_variant = product.selected_or_first_available_variant %}
<h1>{{ product.title | escape }}</h1>
<p>{{ current_variant.title | escape }} — {{ current_variant.price | money_with_currency }}</p>
<form action="{{ product.url }}" method="get">
  <label for="Variant-{{ section.id }}">Variant</label>
  <select id="Variant-{{ section.id }}" name="variant">
    {% for variant in product.variants %}
      <option value="{{ variant.id }}"{% if variant.id == current_variant.id %} selected{% endif %}>
        {{ variant.title | escape }}{% unless variant.available %} — Sold out{% endunless %}
      </option>
    {% endfor %}
  </select>
  <button type="submit">View this variant</button>
</form>
{% assign purchase_form_id = 'Purchase-' | append: section.id %}
{% form 'product', product, id: purchase_form_id %}
  <input type="hidden" name="id" value="{{ current_variant.id }}">
  <button type="submit"{% unless current_variant.available %} disabled{% endunless %}>
    {% if current_variant.available %}Add to cart{% else %}Sold out{% endif %}
  </button>
{% endform %}

验收检查

  • 打开带 variant 参数的链接,核价格与表单 ID。
  • 核缺货、选择变化和 JavaScript 禁用。

这是少量变体的无脚本基线;高变体商品沿当前主题的选项与按需渲染方案实现,不把此循环当全量变体架构。

先理解,再整合,最后验证

  1. 选择模块,确认数据和输入输出。
  2. 将片段接入当前主题或框架。
  3. 用真实变体、市场和失败恢复场景验收。

示例按 Shopify Online Store 2.0 主题编写;Headless 使用单独的数据与版本边界。代码片段需接实际字段、主题和语言文件。

架构参考

示例由 WESWOO 编写,结合 Shopify 官方文档和固定版本的仓库参考。采用仓库代码时应保留适用许可。

Shopify/dawn · 许可commit 3e9ee2281dc4 · Shopify 集成主题的限定许可;参考架构,不复制仓库实现。
Shopify/hydrogen · 许可commit 9e358a2462ba · MIT;采用实质代码时保留版权与许可。

依据核对 2026-11-12