<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ragic &#8211; 小豬日常</title>
	<atom:link href="https://piglife.tw/tag/ragic/feed/" rel="self" type="application/rss+xml" />
	<link>https://piglife.tw</link>
	<description>Hello World，一個紀錄生活與學習的地方</description>
	<lastBuildDate>Fri, 12 Dec 2025 14:48:42 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://piglife.tw/wp-content/uploads/2017/10/cropped-logo-1-32x32.png</url>
	<title>Ragic &#8211; 小豬日常</title>
	<link>https://piglife.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>利用 WordPress 實現 WooCommerce 與 Ragic 訂單資料自動同步技術筆記</title>
		<link>https://piglife.tw/technical-notes/woocommerce-ragic-data-sync/</link>
					<comments>https://piglife.tw/technical-notes/woocommerce-ragic-data-sync/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Tue, 31 Dec 2024 03:19:23 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[api同步]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ragic]]></category>
		<category><![CDATA[woocommerce]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[訂單管理]]></category>
		<category><![CDATA[資料整合]]></category>
		<guid isPermaLink="false">https://piglife.tw/?p=438</guid>

					<description><![CDATA[說明如何利用 WordPress 與 WooCommerce 將訂單資料自動同步至 Ragic 雲端...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在電子商務系統中，訂單資料的管理與整合是提升營運效率的重要環節。WooCommerce 作為 WordPress 上熱門的電商插件，能有效管理訂單，但若要進一步分析與整合資料，將訂單同步至 Ragic 雲端資料庫是一個實用方案。本文適合具備基礎 PHP 與 WordPress 開發經驗的工程師或自學者，說明如何自動將 WooCommerce 訂單資料同步至 Ragic。</p>
<h2 class="wp-block-heading">為什麼要同步 WooCommerce 訂單至 Ragic？</h2>
<p>Ragic 是一款靈活的雲端資料庫工具，方便用戶集中管理與分析業務資料。透過自動同步 WooCommerce 訂單資料，可以：</p>
<ul>
<li>減少手動輸入，提升工作效率</li>
<li>降低人為錯誤，確保資料正確性</li>
<li>集中管理訂單數據，方便後續分析與決策</li>
</ul>
<h2 class="wp-block-heading">教學步驟</h2>
<h3 class="wp-block-heading">1. 啟用訂單完成後的同步觸發</h3>
<p>利用 WooCommerce 的鉤子 <code>woocommerce_order_status_completed</code>，當訂單狀態變更為「已完成」時觸發同步函式：</p>
<pre><code class="lang-php language-php php">add_action(&#039;woocommerce_order_status_completed&#039;, &#039;sync_order_to_ragic&#039;);</code></pre>
<h3 class="wp-block-heading">2. 擷取 WooCommerce 訂單資料</h3>
<p>使用 WooCommerce API 取得訂單詳細資訊，包括用戶姓名、電話、地址及訂單商品：</p>
<pre><code class="lang-php language-php php">$order = wc_get_order($order_id);
$email = $order-&gt;get_billing_email();
$first_name = $order-&gt;get_billing_first_name();
$last_name = $order-&gt;get_billing_last_name();
$phone = $order-&gt;get_billing_phone();
$address = $order-&gt;get_billing_address_1() . &#039;, &#039; . $order-&gt;get_billing_city() . &#039;, &#039; . $order-&gt;get_billing_state() . &#039;, &#039; . $order-&gt;get_billing_postcode();</code></pre>
<h3 class="wp-block-heading">3. 整理訂單商品資料格式</h3>
<p>將訂單中每項商品名稱、數量與價格組合成字串，方便傳輸：</p>
<pre><code class="lang-php language-php php">$items = [];
foreach ($order-&gt;get_items() as $item) {
  $items[] = $item-&gt;get_name() . &#039; x &#039; . $item-&gt;get_quantity() . &#039; (&#039; . $item-&gt;get_total() . &#039;)&#039;;
}
$items_string = implode(&#039;; &#039;, $items);</code></pre>
<h3 class="wp-block-heading">4. 組織符合 Ragic API 欄位結構的資料</h3>
<p>根據 Ragic 表單欄位 ID，將資料映射成陣列格式：</p>
<pre><code class="lang-php language-php php">$ragic_data = [
  &#039;1000686&#039; =&gt; &#039;網站&#039;,
  &#039;1000595&#039; =&gt; $first_name . &#039; &#039; . $last_name,
  &#039;1000596&#039; =&gt; $phone,
  &#039;1000563&#039; =&gt; $address,
  &#039;1000634&#039; =&gt; $email,
  &#039;1000597&#039; =&gt; $items_string,
  &#039;1000610&#039; =&gt; $order-&gt;get_total(),
  &#039;1000594&#039; =&gt; $order-&gt;get_date_created()-&gt;date(&#039;Y-m-d H:i:s&#039;),
];</code></pre>
<h3 class="wp-block-heading">5. 使用 Ragic API 同步資料</h3>
<p>透過 <code>wp_remote_post</code> 發送 POST 請求，將資料以 JSON 格式送至 Ragic：</p>
<pre><code class="lang-php language-php php">$response_ragic = wp_remote_post(
  $ragic_url,
  [
    &#039;method&#039; =&gt; &#039;POST&#039;,
    &#039;headers&#039; =&gt; [
      &#039;Content-Type&#039; =&gt; &#039;application/json&#039;,
      &#039;Authorization&#039; =&gt; &#039;Basic YOUR_RAGIC_API_CREDENTIAL&#039;,
    ],
    &#039;body&#039; =&gt; json_encode($ragic_data),
  ]
);</code></pre>
<h3 class="wp-block-heading">6. 檢查 API 回應並記錄錯誤</h3>
<p>確保同步成功，並在失敗時記錄錯誤訊息以利除錯：</p>
<pre><code class="lang-php language-php php">if (is_wp_error($response_ragic)) {
  error_log(&#039;Ragic 同步失敗：&#039; . $response_ragic-&gt;get_error_message());
} else {
  $status_code_ragic = wp_remote_retrieve_response_code($response_ragic);
  $body_ragic = wp_remote_retrieve_body($response_ragic);

  if ($status_code_ragic == 200 || $status_code_ragic == 201) {
    error_log(&#039;Ragic 同步成功：&#039; . $body_ragic);
  } else {
    error_log(&quot;Ragic API 回應錯誤 (狀態碼: $status_code_ragic)：$body_ragic&quot;);
  }
}</code></pre>
<h2 class="wp-block-heading">注意事項</h2>
<ol>
<li><strong>API Key 安全性</strong>：避免將 API Key 明文寫入程式碼，建議使用環境變數或安全存取方式。</li>
<li><strong>欄位對應檢查</strong>：確認 Ragic 表單欄位 ID 與程式碼中對應正確，避免資料錯置。</li>
<li><strong>測試與效能優化</strong>：正式上線前充分測試同步流程，確保資料完整且效能穩定。</li>
</ol>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-text language-text text">add_action(&#039;woocommerce_order_status_completed&#039;, &#039;sync_order_to_ragic&#039;);

function sync_order_to_ragic($order_id) {
  $order = wc_get_order($order_id);
  $email = $order-&gt;get_billing_email();
  $first_name = $order-&gt;get_billing_first_name();
  $last_name = $order-&gt;get_billing_last_name();
  $phone = $order-&gt;get_billing_phone();
  $address = $order-&gt;get_billing_address_1() . &#039;, &#039; . $order-&gt;get_billing_city() . &#039;, &#039; . $order-&gt;get_billing_state() . &#039;, &#039; . $order-&gt;get_billing_postcode();

  $items = [];
  foreach ($order-&gt;get_items() as $item) {
    $items[] = $item-&gt;get_name() . &#039; x &#039; . $item-&gt;get_quantity() . &#039; (&#039; . $item-&gt;get_total() . &#039;)&#039;;
  }
  $items_string = implode(&#039;; &#039;, $items);

  $ragic_data = [
    &#039;1000686&#039; =&gt; &#039;網站&#039;,
    &#039;1000595&#039; =&gt; $first_name . &#039; &#039; . $last_name,
    &#039;1000596&#039; =&gt; $phone,
    &#039;1000563&#039; =&gt; $address,
    &#039;1000634&#039; =&gt; $email,
    &#039;1000597&#039; =&gt; $items_string,
    &#039;1000610&#039; =&gt; $order-&gt;get_total(),
    &#039;1000594&#039; =&gt; $order-&gt;get_date_created()-&gt;date(&#039;Y-m-d H:i:s&#039;),
  ];

  $ragic_url = &#039;https://www.ragic.com/your_ragic_url&#039;; // 請替換為實際 Ragic API URL

  $response_ragic = wp_remote_post(
    $ragic_url,
    [
      &#039;method&#039; =&gt; &#039;POST&#039;,
      &#039;headers&#039; =&gt; [
        &#039;Content-Type&#039; =&gt; &#039;application/json&#039;,
        &#039;Authorization&#039; =&gt; &#039;Basic YOUR_RAGIC_API_CREDENTIAL&#039;, // 請替換為實際授權資訊
      ],
      &#039;body&#039; =&gt; json_encode($ragic_data),
    ]
  );

  if (is_wp_error($response_ragic)) {
    error_log(&#039;Ragic 同步失敗：&#039; . $response_ragic-&gt;get_error_message());
  } else {
    $status_code_ragic = wp_remote_retrieve_response_code($response_ragic);
    $body_ragic = wp_remote_retrieve_body($response_ragic);

    if ($status_code_ragic == 200 || $status_code_ragic == 201) {
      error_log(&#039;Ragic 同步成功：&#039; . $body_ragic);
    } else {
      error_log(&quot;Ragic API 回應錯誤 (狀態碼: $status_code_ragic)：$body_ragic&quot;);
    }
  }
}</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/woocommerce-ragic-data-sync/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
