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

Shopify应用页面跳转主题编辑器:获取当前活跃主题ID方案

如何获取Shopify当前活跃主题ID并跳转至主题编辑器?

我正在开发一款给Shopify店铺首页新增功能的简易应用,需要在应用说明页添加跳转至主题编辑器的链接。用了App Bridge的Redirect功能,但页面代码里拿不到当前活跃主题的ID,这是我第一次做Shopify开发,求建议。

初始代码(getActiveThemeId暂未实现)

useEffect(() => {
    const app = createApp(config);
    const themeEditorButton = document.getElementById("theme-editor-button");
    themeEditorButton.addEventListener("click", async () => {
      const themeId = await getActiveThemeId();
      const redirect = Redirect.create(app);
      redirect.dispatch(Redirect.Action.ADMIN_PATH, {
        path: "/themes/"+ themeId +"/editor",
        newContext: false,
      });
    });
  }, []); 

解决方法

我用的是Shopify官方提供的应用模板,该模板自带useAppQuery钩子用于调用Admin API,通过这个钩子获取/api/theme/current接口的数据,就能拿到当前活跃主题的ID,最终实现跳转功能,代码如下:

const {
    data: theme,
    isLoading: isLoadingThemeData,
  } = useAppQuery({
    url: "/api/theme/current"
  });

  const goToThemeEditor = () =>{
    const app = createApp(config);
    const redirect = Redirect.create(app);
    redirect.dispatch(
      Redirect.Action.ADMIN_PATH,
      `/themes/${isLoadingThemeData ? "-" : theme.id}/editor`
    );
  }

// 页面按钮部分
<button onClick={goToThemeEditor}>
  {isLoadingThemeData ? "..." : "Edit Theme "+theme.name}
</button>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 14:31:02