案例作品集 浏览精选项目

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

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

商品详情与组合购买

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

适配配件与明确加购

兼容关系来自审核过的型号资料,先列可购买的配件入口。组合加购时重新确认主变体、配件变体和数量,用真实服务端结果更新购物车。

数据与依赖

  • custom.compatible_accessories:list.product_reference。
  • 精确到变体的适配关系需要单独记录。

交互逻辑

  • 切换主型号后重新核配件选择。
  • 提交中防重复;失败不自动再次加购。
示例代码 · snippets/ww-accessories.liquid
Liquid · WESWOO
{% assign accessories = product.metafields.custom.compatible_accessories.value %}
{% if accessories != blank %}
  <section aria-labelledby="Accessories-{{ section.id }}">
    <h2 id="Accessories-{{ section.id }}">Accessories for this model</h2>
    <ul>
      {% for accessory in accessories %}
        <li>
          <a href="{{ accessory.url | escape }}">{{ accessory.title | escape }}</a>
          <span>{{ accessory.price | money_with_currency }}</span>
        </li>
      {% endfor %}
    </ul>
  </section>
{% endif %}

示例代码 · assets/ww-add-selection.js
JavaScript · WESWOO
export async function addSelection(root, items) {
  if (!root.startsWith('/') || root.startsWith('//')) throw new Error('Invalid store route');
  if (!items.length || items.some(item => !Number.isSafeInteger(item.id) || item.id <= 0 ||
      !Number.isSafeInteger(item.quantity) || item.quantity <= 0)) {
    throw new Error('Select valid variants and quantities');
  }
  const response = await fetch(`${root}cart/add.js`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json', 'Accept': 'application/json'},
    body: JSON.stringify({items})
  });
  const result = await response.json();
  if (!response.ok) throw new Error(result.description || 'The selection could not be added');
  return result;
}
// Pass window.Shopify.routes.root from a Shopify theme.
// Disable repeat submission while pending, then refresh actual cart state.
// A network error must not trigger an automatic second add request.

验收检查

  • 核不兼容、缺货及多个配件数量。
  • 成功或异常后读取购物车确认实际行项目。

商品推荐不等于兼容证明。列表片段不直接加购;Ajax 函数须接已有状态反馈与实际路由。

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

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

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

架构参考

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

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

依据核对 2026-11-12