无头电商中文封面,突出增长检查主题

无头电商

摘要

25 无头电商

先判断问题出现在哪里

Shopify 的原生前端(Liquid)太重、太慢、受限太多。无头电商(Headless)就是把 Shopify 的“头(前端界面)”砍掉,只用它的“身体(后台管理和结账)”。前端用 Next.js 重写,实现毫秒级加载和完全自由的交互体验。

无头电商的四项 Shopify 检查清单
无头电商的四项 Shopify 检查清单

实战步骤

步骤 1:配置 Shopify Storefront API 权限

无头架构下,前端无法直接读取 Liquid 变量,必须通过 Storefront API 获取产品数据。

操作路径Shopify后台 -> 设置 -> 应用和销售渠道 -> 开发应用 -> 创建应用

  1. 命名为“Nextjs-Headless-Front”。

  2. 点击 配置 Storefront API 范围,勾选 unauthenticated_read_product_listings(读取产品)、unauthenticated_write_checkouts(创建结账)等必要权限。

  3. 安装应用后,复制生成的 Storefront API 访问令牌 (Public Access Token)(注意:这个 Token 是公开的,可以安全地放在前端代码中)。

步骤 2:使用 Next.js 抓取商品数据 (GraphQL)

在 Next.js 项目中,通过 GraphQL 向 Shopify 请求数据,并利用 Next.js 的静态生成(SSG)实现秒开。

操作路径本地 Next.js 项目 -> lib/shopify.js

// 封装请求 Shopify Storefront API 的核心函数
export async function shopifyFetch({ query, variables }) {
  const endpoint = `https://${process.env.SHOPIFY_STORE_DOMAIN}/api/2026-01/graphql.json`;
  
  try {
    const result = await fetch(endpoint, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-Shopify-Storefront-Access-Token': process.env.SHOPIFY_STOREFRONT_ACCESS_TOKEN
      },
      body: JSON.stringify({ query, variables })
    });

    return {
      status: result.status,
      body: await result.json()
    };
  } catch (error) {
    console.error('Error:', error);
    return { status: 500, error: 'Error receiving data' };
  }
}

// 获取前 10 个产品的 GraphQL 查询
const getProductsQuery = `
  query getProducts {
    products(first: 10) {
      edges {
        node {
          id
          title
          handle
          priceRange {
            minVariantPrice {
              amount
              currencyCode
            }
          }
          images(first: 1) {
            edges {
              node {
                url
                altText
              }
            }
          }
        }
      }
    }
  }
`;

步骤 3:构建购物车并跳转至 Shopify 结账页

无头电商最难的部分是结账。不要自己写结账逻辑(极度不安全且违规),必须调用 API 生成 Checkout URL,然后把用户踢回 Shopify 的官方结账页。

操作路径调用 checkoutCreate Mutation

// 创建结账并返回结账链接的 GraphQL
const createCheckoutMutation = `
  mutation checkoutCreate($input: CheckoutCreateInput!) {
    checkoutCreate(input: $input) {
      checkout {
        id
        webUrl # 这是 Shopify 生成的官方结账链接
      }
    }
  }
`;

// 当用户点击 "Checkout" 按钮时触发:
// 1. 将购物车里的商品 ID 和数量传入 mutation
// 2. 拿到 webUrl
// 3. window.location.href = webUrl; (跳转去付款)

常见误区与处理方法

误区一:盲目上马 Headless,导致运营团队彻底瘫痪

老板听信了技术外包的忽悠,花了 5 万美金做了一套 Headless 网站。结果上线后发现:运营人员再也无法使用 Shopify 后台的主题编辑器(Theme Editor)了! 想换一张首页 Banner 图、改一个按钮颜色,都必须提需求给程序员改代码重新部署。

规避方法:如果你的团队没有全职的 React/Next.js 工程师,绝对不要碰 Headless! 如果非要做,必须同时引入 Sanity 或 Builder.io 等 Headless CMS(内容管理系统),把前端的图片、文案配置权重新交还给运营团队。

误区二:Shopify App Store 里的插件全部失效

做完 Headless 后,你发现之前在 Shopify 后台买的商品评论插件(Loox)、倒计时插件、弹窗插件全部不工作了。因为这些插件是基于 Liquid 注入代码的,现在前端没有 Liquid 了。

规避方法:在决定做 Headless 之前,必须盘点你当前依赖的所有第三方 App。只有提供完整 REST/GraphQL API 的 App,才能在 Headless 架构下继续使用。 比如评论系统必须换成支持 API 调用的 Yotpo 或 Okendo,并且需要前端工程师重新写一遍 UI 组件来渲染这些评论数据。开发成本极高,入坑需谨慎。

无头电商从判断到验证的三步执行路径
无头电商从判断到验证的三步执行路径

FAQ

无头电商应该先检查什么?

先确认业务阶段、数据基础和当前最需要解决的一个问题。不要同时改很多位置,先记录当前页面和数据,再处理最明确的问题。

需要马上安装新的 Shopify App 吗?

不一定。先判断主题现有功能、后台字段和少量代码能否解决。只有需要持续同步数据或复杂自动化时,再评估 App 的费用、脚本负担和卸载影响。

修改后怎么验证是否有效?

记录修改日期、页面 URL 和改动内容,再用实际页面、移动端、Google Search Console、Bing Webmaster Tools 或 GA4 检查结果。技术修改还要保留测试记录和回滚版本。

哪些情况不建议马上修改?

数据量太少、追踪没有配置、问题还没有复现,或者正在进行大型主题更新时,不建议一次性重做。先把问题拆开,确认影响范围后再改。

下一步阅读

📢 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