<?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>訂單資料 &#8211; 小豬日常</title>
	<atom:link href="https://piglife.tw/tag/%E8%A8%82%E5%96%AE%E8%B3%87%E6%96%99/feed/" rel="self" type="application/rss+xml" />
	<link>https://piglife.tw</link>
	<description>Hello World，一個紀錄生活與學習的地方</description>
	<lastBuildDate>Fri, 12 Dec 2025 14:49:43 +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>訂單資料 &#8211; 小豬日常</title>
	<link>https://piglife.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>使用 PHP 記錄 WooCommerce 訂單更新資料結構技術筆記</title>
		<link>https://piglife.tw/technical-notes/php-log-woocommerce-order-data/</link>
					<comments>https://piglife.tw/technical-notes/php-log-woocommerce-order-data/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Thu, 09 Jan 2025 12:49:56 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[hook]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[woocommerce]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[訂單資料]]></category>
		<category><![CDATA[除錯]]></category>
		<guid isPermaLink="false">https://piglife.tw/?p=468</guid>

					<description><![CDATA[說明如何使用 PHP 透過 WooCommerce 的訂單狀態變更鉤子，將訂單詳細資料以 JSON ...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在開發基於 WordPress 的 WooCommerce 網站時，了解訂單資料結構對於客製化功能非常重要。當訂單狀態或詳細資料更新時，能夠即時記錄這些資料有助於除錯與後續分析。本文針對 WooCommerce 訂單更新事件，說明如何使用 PHP 透過鉤子（hook）將訂單資料以 JSON 格式記錄到檔案中。</p>
<h2 class="wp-block-heading">需求情境</h2>
<ol>
<li>監聽 WooCommerce 訂單更新事件。</li>
<li>獲取訂單的完整詳細資料。</li>
<li>將資料儲存為 JSON 格式，方便後續分析或除錯。</li>
</ol>
<h2 class="wp-block-heading">解決方案</h2>
<p>利用 WooCommerce 提供的 <code>woocommerce_order_status_changed</code> 鉤子，在訂單狀態變更時觸發自訂函數，取得訂單資料並寫入檔案。</p>
<pre><code class="lang-php language-php php">add_action(&#039;woocommerce_order_status_changed&#039;, &#039;log_order_data&#039;, 10, 4);

function log_order_data($order_id, $old_status, $new_status, $order) {
    $data = [
        &#039;order_id&#039; =&gt; $order_id,
        &#039;old_status&#039; =&gt; $old_status,
        &#039;new_status&#039; =&gt; $new_status,
        &#039;order_data&#039; =&gt; $order-&gt;get_data() // 獲取訂單詳細資料
    ];

    write_to_log_file($data, &#039;order_update_log.json&#039;);
}

function write_to_log_file($data, $filename = &#039;meta_data.json&#039;) {
    $log_dir = __DIR__ . &#039;/logs&#039;; // 指定記錄檔存放目錄
    if (!file_exists($log_dir)) {
        mkdir($log_dir, 0755, true); // 如果目錄不存在，創建目錄
    }
    $log_file = $log_dir . &#039;/&#039; . $filename;

    // 將資料轉換為 JSON 格式
    $json_data = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

    // 將 JSON 資料寫入文件（直接覆寫內容）
    file_put_contents($log_file, $json_data);
}</code></pre>
<h2 class="wp-block-heading">程式碼解說</h2>
<ol>
<li>
<p><strong>監聽訂單狀態更新事件</strong>
使用 WooCommerce 的 <code>woocommerce_order_status_changed</code> 鉤子，當訂單狀態變更時觸發 <code>log_order_data</code> 函數。</p>
<pre><code class="lang-php language-php php">add_action(&#039;woocommerce_order_status_changed&#039;, &#039;log_order_data&#039;, 10, 4);</code></pre>
</li>
<li>
<p><strong>獲取訂單詳細資料</strong>
透過 <code>WC_Order</code> 物件的 <code>get_data()</code> 方法取得完整訂單資料，包含客戶資訊、商品清單、付款方式等。</p>
<pre><code class="lang-php language-php php">&#039;order_data&#039; =&gt; $order-&gt;get_data()</code></pre>
</li>
<li>
<p><strong>記錄資料至 JSON 檔案</strong>
將資料轉換為 JSON 格式，並寫入指定的 <code>logs</code> 目錄下的檔案中，若目錄不存在則自動建立。</p>
<pre><code class="lang-php language-php php">write_to_log_file($data, &#039;order_update_log.json&#039;);</code></pre>
</li>
</ol>
<h2 class="wp-block-heading">範例輸出</h2>
<p>假設訂單狀態從 <code>processing</code> 更新為 <code>completed</code>，記錄的 JSON 資料範例如下：</p>
<pre><code class="lang-json language-json json">{
  &quot;order_id&quot;: 1234,
  &quot;old_status&quot;: &quot;processing&quot;,
  &quot;new_status&quot;: &quot;completed&quot;,
  &quot;order_data&quot;: {
    &quot;id&quot;: 1234,
    &quot;status&quot;: &quot;completed&quot;,
    &quot;currency&quot;: &quot;USD&quot;,
    &quot;total&quot;: &quot;99.99&quot;,
    &quot;customer_id&quot;: 5678,
    &quot;billing&quot;: {
      &quot;first_name&quot;: &quot;John&quot;,
      &quot;last_name&quot;: &quot;Doe&quot;,
      &quot;email&quot;: &quot;john.doe@example.com&quot;
    },
    &quot;line_items&quot;: [
      {
        &quot;name&quot;: &quot;T-Shirt&quot;,
        &quot;quantity&quot;: 2,
        &quot;total&quot;: &quot;49.99&quot;
      }
    ]
  }
}</code></pre>
<h2 class="wp-block-heading">適用場景</h2>
<ul>
<li><strong>除錯需求</strong>：快速了解 WooCommerce 訂單資料結構，協助定位問題。</li>
<li><strong>數據分析</strong>：將記錄的 JSON 資料用於訂單數據分析。</li>
<li><strong>客製化功能開發</strong>：根據訂單詳細資料設計特定邏輯。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-text language-text text">add_action(&#039;woocommerce_order_status_changed&#039;, &#039;log_order_data&#039;, 10, 4);

function log_order_data($order_id, $old_status, $new_status, $order) {
    $data = [
        &#039;order_id&#039; =&gt; $order_id,
        &#039;old_status&#039; =&gt; $old_status,
        &#039;new_status&#039; =&gt; $new_status,
        &#039;order_data&#039; =&gt; $order-&gt;get_data() // 獲取訂單詳細資料
    ];

    write_to_log_file($data, &#039;order_update_log.json&#039;);
}

function write_to_log_file($data, $filename = &#039;meta_data.json&#039;) {
    $log_dir = __DIR__ . &#039;/logs&#039;; // 指定記錄檔存放目錄
    if (!file_exists($log_dir)) {
        mkdir($log_dir, 0755, true); // 如果目錄不存在，創建目錄
    }
    $log_file = $log_dir . &#039;/&#039; . $filename;

    // 將資料轉換為 JSON 格式
    $json_data = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

    // 將 JSON 資料寫入文件（直接覆寫內容）
    file_put_contents($log_file, $json_data);
}</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/php-log-woocommerce-order-data/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
