You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopify Warehouse主题导入main-collection到index.json分页报错如何解决

问题根源

main-collection.liquid是为集合详情页设计的模板,集合详情页会自动注入全局collection对象对应当前访问的集合数据。但首页没有默认的全局collection对象,你直接将该section挂载到首页后,模板读取到的collection为非合法集合对象,导致collection.products不是可分页的数组,触发报错。

修复步骤
  • 步骤1:调整main-collection.liquid的配置项
    打开sections/main-collection.liquid文件,找到顶部的{% schema %}代码块,在settings数组中新增集合选择配置,原有其他配置保留即可:
    {% schema %}
    {
      "name": "首页集合展示",
      "settings": [
        // 原有配置全部保留,新增以下配置项
        {
          "type": "collection",
          "id": "target_collection",
          "label": "选择要展示的集合"
        }
      ]
    }
    {% endschema %}
    
  • 步骤2:替换模板内的集合调用变量
    在模板内分页代码前新增变量赋值语句,同时把模板中所有调用collection的位置全部替换为自定义的target_collection变量:
    // 新增在分页代码前
    {% assign target_collection = collections[section.settings.target_collection] %}
    // 修改原分页代码
    {%- paginate target_collection.products by items_per_page -%}
    
    注意:模板内其他类似collection.title、collection.description等调用,都要同步替换为target_collection.xxx,避免其他关联报错。
  • 步骤3:更新index.json配置
    在index.json的main对应settings中添加你要展示的集合handle,比如要展示全站所有商品可以填默认集合handle all:
    {
      "sections": {
        "main": {
          "type": "main-collection",
          "settings": {
            // 原有其他settings全部保留,新增以下行
            "target_collection": "all",
            "show_collection_image": false,
            ... // 原有配置保持不变
          }
        }
      }
    }
    

可选优化:如果不想影响原有集合详情页的使用,可以复制main-collection.liquid重命名为home-collection.liquid,在新文件中做上述修改,index.json中对应的type改为home-collection即可。

内容的提问来源于stack exchange,提问作者Jessica

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 01:30:02