Shopify Liquid 进阶教程:高级语法与自定义 Section 开发 封面图

Shopify Liquid 进阶教程:高级语法与自定义 Section 开发

摘要

想突破主题的限制?掌握 Liquid 让你随心所欲改代码!拆解高级语法、动态数据过滤与性能优化,分步骤带你开发专属的自定义模块。

先判断问题出现在哪里

不懂 Liquid,你只能受限于主题的死板框架。掌握 Liquid,你能直接调用 Shopify 数据库里的任何字段,把静态页面变成千人千面的动态营销机器。

实战步骤

步骤 1:开发带倒计时的自定义促销 Section

Shopify Liquid 进阶教程:高级语法与自定义 Section 开发:步骤 1:开发带倒计时的自定义促销 Section
真实页面参考:Shopify 性能优化官方文档。对照本步骤确认当前官方路径和关键概念,实际操作以你的店铺后台、本地终端或代码仓库为准。
Shopify Liquid 进阶教程:高级语法与自定义 Section 开发:步骤 3:使用 paginate 标签优化海量数据加载
真实页面参考:Shopify 性能优化官方文档。对照本步骤确认当前官方路径和关键概念,实际操作以你的店铺后台、本地终端或代码仓库为准。

不要花每个月 $19 去买倒计时插件,自己写一个 Section,速度快 10 倍且完全免费。

操作路径Shopify后台 -> 在线商店 -> 主题 -> 编辑代码 -> sections -> 添加新 section (命名为 custom-promo-banner.liquid)

{% # 1. 定义 Schema 让商家可以在后台配置 %}
{% schema %}
{
  "name": "促销倒计时横幅",
  "settings": [
    {
      "type": "text",
      "id": "promo_text",
      "label": "促销文案",
      "default": "Flash Sale! 20% OFF ends in:"
    },
    {
      "type": "text",
      "id": "end_date",
      "label": "结束时间 (格式: YYYY-MM-DD HH:MM)",
      "default": "2026-12-31 23:59"
    }
  ],
  "presets": [{"name": "促销倒计时横幅"}]
}
{% endschema %}

{% # 2. 渲染 HTML 结构 %}
<div class="custom-promo-banner" style="background: #ef4444; color: white; text-align: center; padding: 10px;">
  <span>{{ section.settings.promo_text }}</span>
  <strong id="countdown-timer-{{ section.id }}" data-end="{{ section.settings.end_date }}"></strong>
</div>

{% # 3. 注入倒计时 JS 逻辑 %}
<script>
  document.addEventListener("DOMContentLoaded", function() {
    const timerEl = document.getElementById('countdown-timer-{{ section.id }}');
    const endDate = new Date(timerEl.getAttribute('data-end')).getTime();
    
    const x = setInterval(function() {
      const now = new Date().getTime();
      const distance = endDate - now;
      if (distance < 0) {
        clearInterval(x);
        timerEl.innerHTML = "EXPIRED";
        return;
      }
      const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
      const seconds = Math.floor((distance % (1000 * 60)) / 1000);
      timerEl.innerHTML = hours + "h " + minutes + "m " + seconds + "s ";
    }, 1000);
  });
</script>

步骤 2:利用 Liquid 过滤器 (Filters) 动态处理数据

Shopify Liquid 进阶教程:高级语法与自定义 Section 开发:步骤 2:利用 Liquid 过滤器 (Filters) 动态处理数据
真实页面参考:Shopify 性能优化官方文档。对照本步骤确认当前官方路径和关键概念,实际操作以你的店铺后台、本地终端或代码仓库为准。

不要在后台手动算折扣价,让系统自动算。

操作路径在 product.liquid 或 product-card.liquid 中使用

{% # 计算并显示动态折扣百分比 %}
{% if product.compare_at_price > product.price %}
  {% assign discount_amount = product.compare_at_price | minus: product.price %}
  {% assign discount_percent = discount_amount | times: 100.0 | divided_by: product.compare_at_price | round %}
  
  <span class="badge-sale" style="background: red; color: white;">
    Save {{ discount_percent }}% ({{ discount_amount | money }})
  </span>
{% endif %}

步骤 3:使用 paginate 标签优化海量数据加载

如果一个集合有 500 个产品,一次性加载会让页面直接卡死。

操作路径在 collection.liquid 中使用分页

{% # 严格限制每页只加载 24 个产品 %}
{% paginate collection.products by 24 %}
  <div class="product-grid">
    {% for product in collection.products %}
      {% render 'product-card', product: product %}
    {% endfor %}
  </div>
  
  {% # 渲染底部分页按钮 %}
  {% if paginate.pages > 1 %}
    {{ paginate | default_pagination }}
  {% endif %}
{% endpaginate %}

常见误区与处理方法

误区一:在 for 循环中滥用 assign 导致内存溢出

在遍历 1000 个订单或客户数据时,在循环内部大量定义新变量(assign),会导致 Shopify 服务器渲染超时(Liquid Error: Memory limits exceeded),页面直接白屏报错。

规避方法:尽量在循环外部定义好变量。如果必须在循环内过滤数据,优先使用 where 过滤器直接在数组层面筛选,而不是用 for + if 逐个判断。例如:{% assign available_products = collection.products | where: "available", true %}

误区二:直接修改主题核心文件,导致无法升级

为了加个小功能,直接在 theme.liquidglobal.js 里乱改一通。半年后主题出了修复安全漏洞的新版本,你一升级,所有自定义代码全部被覆盖清空。

规避方法绝对不要直接修改主题自带的核心文件! 必须使用 自定义 SectionSnippets(代码片段),或者在后台主题编辑器中通过 Custom CSS / Custom Liquid 区块 注入代码。这样即使主题升级,你的自定义模块也能无损迁移。

常见问题

Liquid 进阶多久能看到效果?

SEO 和 GEO 通常不是当天见效的工作。更合理的做法是先保证页面结构、标题、内链和内容深度正确,再按 4 到 8 周观察收录、曝光和自然点击变化。

文章里要不要堆关键词?

不要。标题、首段、小标题和 FAQ 里自然覆盖用户真实问题即可。重复堆词会降低可读性,也不利于 AI 摘要准确理解页面。

Liquid 进阶应该先看哪个核心指标?

先看能直接影响决策的指标,不要只看曝光或访问量。新手可以把转化率、获客成本、客单价、复购或退款情况放在同一张表里,每周复盘一次。

做Liquid 进阶前需要准备什么?

先确认目标、当前数据、页面或后台路径,再准备一份改动记录。这样出现波动时能追溯原因,也方便后续把有效动作沉淀成 SOP。

Liquid 进阶多久复盘一次比较合适?

运营类动作建议每周小复盘、每月大复盘;广告或转化测试不要因为单日波动频繁改动,至少等到有足够样本后再判断。

新手最容易踩的坑是什么?

最常见的问题是同时改太多变量,最后不知道是哪一步带来结果。每次只改一个关键点,保留截图、数据和发布时间,后续才有可复用的经验。

下一步阅读

0 comments

Leave a comment

Please note, comments need to be approved before they are published.

📢 Share this article

Any other questions?

Our professional team is ready to answer your questions.

Was this article helpful to me?

This article is suitable for all merchants and developers who want to learn about Shopify. Whether you are a beginner just starting out with Shopify or an advanced user looking to improve your skills, you will gain practical knowledge and techniques from it. The methods in this article have all been tested and proven in practice and can be directly applied to your projects.

How can we apply the methods described in the article?

Each step in this article comes with detailed instructions and code examples, which you can directly copy and use. It's recommended to try it in a test environment first to confirm the results before applying it to the production site. If you encounter any problems during implementation, feel free to leave a comment or join our discussion group for help; we and our community members will be happy to assist you.

Can the code in the article be used directly?

Yes! All the code examples we provide have been tested and can be used directly in your Shopify theme. Remember to adjust the parameters and styles according to your actual needs. If you encounter any problems, feel free to leave a message for discussion.

How often will new content be updated?

We publish 2-3 high-quality Shopify tutorials and operational tips every week. Follow our WeChat official account or join our discussion group to get the latest content and exclusive resources first.

Can I get help if I encounter a problem?

Of course! You can leave a comment below the article or join our WeChat group to connect with 1000+ Shopify merchants and developers. We'll get back to you as soon as possible.

Ready to get started?

Follow us to get the latest Shopify tutorials and operational tips.

Join the community Contact Us