Builder.io 與 Shopify 主題整合指南

Builder.io 與 Shopify 主題整合指南

整合方法概述

Builder.io 為 Shopify 商店提供多種整合選項,讓不同技術背景的用戶都能找到適合的解決方案。從直接嵌入程式碼到使用插件,Builder.io 讓電子商務網站建設變得更加靈活高效。

嵌入式整合方法

最直接的整合方式是將 Builder.io 內容嵌入到 Shopify 主題檔案中。這種方法涉及添加自訂程式碼到 Shopify 模板:

{% comment %}
添加此代碼片段到您的 Shopify 主題以渲染 Builder 區段
{% endcomment %}
<div id="builder-section">
  <script>
  window.builderApiKey = 'YOUR_API_KEY';
  </script>
  <div id="builder-component" data-builder-component="custom-section"></div>
</div>
<script>
(function() {
  var script = document.createElement('script');
  script.src = 'https://cdn.builder.io/js/webcomponents';
  script.async = true;
  document.head.appendChild(script);
})();
</script>

這段代碼將 Builder.io 創建的內容動態加載到您的 Shopify 主題中。您只需將「YOUR_API_KEY」替換為您實際的 Builder.io 公開 API 密鑰,並設置適當的組件名稱即可。

Shopify 插件設置

Builder.io 提供了官方 Shopify 插件,使整合過程更加簡化:

  1. 進入 Builder.io 中的「Integrations」頁面
  2. 點擊 Shopify 插件的「Enable」按鈕
  3. 完成啟用後,點擊通知中的「Configure」按鈕
  4. 提供您的「storefront access token」和「store domain」
  5. 點擊「Connect Your Shopify Custom App」按鈕完成連接

使用這種方法,您需要在 Shopify 中啟用自定義(以前稱為「私有」)應用程序開發功能。設置完成後,您可以將 Shopify 商店作為 Builder.io 的數據源,訪問產品目錄和其他重要電子商務數據。

創建靜態主題

對於希望創建可直接安裝的 Shopify 主題的開發者,Builder.io 提供了從設計到代碼的路徑:

  1. 使用 Builder.io 視覺編輯器創建和設計頁面或部分
  2. 導出構建好的 HTML 和 CSS 代碼
  3. 將導出的代碼轉換為 Shopify 的 Liquid 模板
  4. 將靜態內容替換為必要的動態 Liquid 標籤

這種方法適合希望創建完全定制的 Shopify 主題而不需要維持 Builder.io 連接的情況。

框架特定整合

對於使用現代框架的商店,Builder.io 提供了特定框架的整合選項:

Next.js 整合

import { BuilderComponent, builder } from '@builder.io/react';

// 初始化 Builder
builder.init('YOUR_API_KEY');

export default function Page() {
  // 獲取 Builder 內容
  const [content, setContent] = useState(null);

  useEffect(() => {
    builder.get('page', { url: window.location.pathname })
      .promise().then(setContent);
  }, []);

  return (
    <>
      <Header /> {/* 保留您自己的頁眉 */}
      <BuilderComponent model="page" content={content} />
      <Footer /> {/* 保留您自己的頁腳 */}
    </>
  );
}

Hydrogen 框架整合

Shopify 的 Hydrogen 框架與 Builder.io 配合使用時,可以創建高性能的無頭商務體驗:

const MODEL_NAME = 'page';

export default function Page(props) {
  const content = useQuery([MODEL_NAME, props.pathname], async () => {
    return await builder
      .get(MODEL_NAME, {
        userAttributes: {
          urlPath: props.pathname,
        },
      })
      .promise();
  });
  
  return (
    <div>
      {!content.data ? (
        <NotFound />
      ) : (
        <Layout>
          <BuilderComponent model={MODEL_NAME} content={content?.data} />
        </Layout>
      )}
    </div>
  );
}

頁面結構與組件整合模式

當整合 Builder.io 與 Shopify 主題時,常見的模式是保留頁眉和頁腳在 Shopify 主題中,而使用 Builder.io 管理頁面中間的內容。這種「三明治」方法讓開發人員能保留對關鍵導航元素的控制,同時允許營銷團隊自主管理頁面內容。

對於特定部分(如產品展示區域或英雄橫幅),可以創建 Builder.io 區段模型,然後通過 Liquid 代碼片段在適當的位置包含這些部分:

{% include 'builder-section.liquid' %}

產品數據整合

為了充分利用 Shopify 產品數據,可以在 Builder.io 中設置動態產品顯示:

  1. Builder.io 中啟用 Shopify 插件
  2. 使用插件創建產品卡片、產品畫廊等
  3. 連接產品 ID、URL 和標題以實現動態顯示

對於 Shopify 應用中的產品頁面,Builder.io 可以自動獲取當前產品的上下文,無需手動設置產品 ID。

代碼提取與性能考量

Builder.io 生成的內容可以導出為乾淨、優化的 Liquid 代碼,確保高性能的網站速度。每個佈局發布時都會自動轉換為優化代碼,支持:

  1. 本地化翻譯(與 Shopify Markets 功能兼容)
  2. Metafield 集成
  3. 實時產品同步

結論

Builder.io 提供多種與 Shopify 主題整合的方法,從簡單的嵌入式代碼到深度框架集成。選擇哪種方法取決於您的技術需求、團隊結構和業務目標。

不論選擇哪種整合方式,Builder.io 都能幫助您創建更具吸引力、更易於管理的 Shopify 商店,讓非技術團隊成員能夠自主更新內容,同時保持網站的高性能和品牌一致性。

隨著 Builder.io 及 Shopify 平台的持續發展,整合選項和功能將繼續擴展,為電子商務網站建設提供更多可能性。

Builder.io 內容轉換為 Shopify Liquid 模板指南

Builder.io 到 Shopify 的轉換流程

Builder.io 中建立的視覺化內容轉換為 Shopify Liquid 模板是一個強大的方法,可讓您創建可直接安裝的靜態主題,而無需維持與 Builder.io 的持續連接。本指南將引導您完成整個轉換過程,幫助您有效地將設計轉化為功能性 Shopify 主題。

設計與建構階段

首先,您需要在 Builder.io 中設計和構建您的頁面或部分:

  1. 使用 Builder.io 視覺編輯器創建並設計您的頁面或部分
  2. 根據您的主題需求自定義內容和樣式
  3. 預覽並測試您的設計,確保它符合您的要求

在這個階段,您可以充分利用 Builder.io 的拖放功能和視覺編輯器來創建理想的設計,而不必擔心代碼層面的細節。

Builder.io 導出代碼

設計完成後,您需要從 Builder.io 導出 HTML 和 CSS 代碼:

  1. 進入您建構的內容頁面
  2. 點擊右上角的三點圖標
  3. 選擇「Export Code」選項
  4. 複製導出的 HTML 和 CSS 代碼

此步驟將為您提供可以轉換為 Liquid 模板的基礎代碼。

將 HTML 和 CSS 轉換為 Liquid 模板

這是整個過程中最關鍵的步驟。您需要將靜態的 HTML 和 CSS 轉換為動態的 Liquid 模板:

  1. 創建新的 Liquid 文件,如 sections/custom-section.liquid
  2. 將導出的 HTML 粘貼到文件中
  3. 將靜態內容替換為動態 Liquid 標籤,使它可以通過 Shopify 管理員進行自定義

例如,將靜態英雄橫幅:

<section class="hero-section">
  <h1 class="hero-title">Welcome to Our Store</h1>
  <p class="hero-description">The best products available online.</p>
  <a href="/collections/all" class="btn btn-primary">Shop Now</a>
</section>
<style>
  .hero-section {
    background: url('banner.jpg') no-repeat center center;
    text-align: center;
    padding: 50px;
  }
  /* 更多 CSS 樣式 */
</style>

轉換為動態 Liquid 模板:

{% if section.settings.background_image %}
<section class="hero-section" style="background: url('{{ section.settings.background_image | img_url: 'master' }}') no-repeat center center;">
{% else %}
<section class="hero-section">
{% endif %}
  <h1 class="hero-title">{{ section.settings.title }}</h1>
  <p class="hero-description">{{ section.settings.description }}</p>
  <a href="{{ section.settings.button_link }}" class="btn btn-primary">{{ section.settings.button_text }}</a>
</section>
<style>
  .hero-section {
    text-align: center;
    padding: 50px;
  }
  /* 保留其他 CSS 樣式 */
</style>

添加設置模式 (Schema)

為了讓店主能夠自定義 Liquid 模板的內容,您需要添加設置模式:

{% schema %}
{
  "name": "Hero Section",
  "settings": [
    {
      "type": "image_picker",
      "id": "background_image",
      "label": "Background Image"
    },
    {
      "type": "text",
      "id": "title",
      "label": "Title",
      "default": "Welcome to Our Store"
    },
    {
      "type": "textarea",
      "id": "description",
      "label": "Description",
      "default": "The best products available online."
    },
    {
      "type": "url",
      "id": "button_link",
      "label": "Button Link",
      "default": "/collections/all"
    },
    {
      "type": "text",
      "id": "button_text",
      "label": "Button Text",
      "default": "Shop Now"
    }
  ],
  "presets": [
    {
      "name": "Hero Section",
      "category": "Custom"
    }
  ]
}
{% endschema %}

這個模式定義了可以在 Shopify 主題自定義器中調整的設置。

創建 Shopify 部分並集成到主題

將轉換後的代碼放入 Shopify 主題的部分(sections)目錄中:

  1. 在您的 Shopify 主題中,進入 sections 目錄
  2. 創建新文件,如 hero-section.liquid
  3. 粘貼完整的轉換代碼(包括 HTML、CSS 和模式)
  4. 保存文件

然後,您可以在 Shopify 模板中引用此部分:

{% section 'hero-section' %}

或者,將其添加到主題自定義器中使其可選。

處理商店數據和動態內容

如果您需要在轉換的部分中包含 Shopify 商店數據(如產品信息),您可以使用 Liquid 標籤來引用這些信息:

{% if product %}
  <h2>{{ product.title }}</h2>
  <p>{{ product.price | money }}</p>
  <div>{{ product.description }}</div>
{% endif %}

這使您的模板能夠顯示來自 Shopify 的動態數據。

使用自定義 Liquid 代碼

如果您需要更複雜的功能,可以在 Shopify 主題中添加自定義 Liquid 元素:

  1. 在 Shopify 管理員面板中,轉到「主題」下的「在線商店」部分
  2. 點擊當前主題旁的「編輯代碼」下拉菜單
  3. 在 Sections 目錄中添加新部分,命名為 “custom-liquid-section”
  4. 添加以下代碼:
{{ section.settings.custom_liquid }}

{% schema %}
{
  "name": "Custom Liquid",
  "settings": [
    {
      "type": "liquid",
      "id": "custom_liquid",
      "label": "Custom Liquid",
      "info": "Add app snippets or other liquid code to create advanced customizations."
    }
  ],
  "presets": [
    {
      "name": "Custom Liquid"
    }
  ]
}
{% endschema %}

這將允許您在主題編輯器中添加自定義 Liquid 代碼。

結論

Builder.io HTML 和 CSS 轉換為 Shopify Liquid 模板是一個多步驟的過程,但它提供了創建靜態、可自定義主題的強大方法。通過遵循這些步驟,您可以創建專業的 Shopify 主題,讓客戶能夠直接安裝到他們的商店,無需維持與 Builder.io 的連接。

雖然這個過程可能需要一些技術知識,但使用 Builder.io 的視覺編輯器作為起點,可以大大簡化設計過程,使您能夠專注於創建美觀、功能豐富的電子商務體驗。