<?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%87%AA%E8%A8%82%E6%AC%84%E4%BD%8D/feed/" rel="self" type="application/rss+xml" />
	<link>https://piglife.tw</link>
	<description>Hello World，一個紀錄生活與學習的地方</description>
	<lastBuildDate>Sun, 28 Dec 2025 22:20:33 +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>WooCommerce 後台訂單自訂欄位資料儲存實作說明</title>
		<link>https://piglife.tw/technical-notes/woocommerce-order-meta-save/</link>
					<comments>https://piglife.tw/technical-notes/woocommerce-order-meta-save/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Sun, 28 Dec 2025 22:20:33 +0000</pubDate>
				<category><![CDATA[技術筆記]]></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/technical-notes/woocommerce-order-meta-save/</guid>

					<description><![CDATA[說明如何在 WooCommerce 後台訂單編輯頁面保存自訂欄位資料，透過安全且兼容新版訂單存儲的方...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在 WooCommerce 訂單後台管理時，常會需要額外新增自訂欄位以收集特定資訊，例如客戶的聯絡電話。這段程式碼示範如何在訂單編輯頁面保存自訂欄位資料，確保資料能正確存入訂單元資料中。適合已熟悉 WooCommerce 且想擴充後台訂單欄位功能的工程師或自學者。</p>
<h2 class="wp-block-heading">為什麼需要自訂欄位儲存機制</h2>
<p>WooCommerce 預設訂單資料結構無法涵蓋所有業務需求，因此經常會透過自訂欄位來擴充。這些欄位必須在後台訂單編輯時能夠被正確讀取與保存，否則資料會遺失。</p>
<h2 class="wp-block-heading">使用 woocommerce_process_shop_order_meta 鉤子</h2>
<p>這個 action 鉤子會在 WooCommerce 處理訂單後台編輯表單時觸發，適合用來攔截並保存自訂欄位資料。</p>
<pre><code class="lang-php language-php php">add_action(&#039;woocommerce_process_shop_order_meta&#039;, function ($order_id) {
    // 權限檢查，避免非授權使用者修改訂單
    if (!current_user_can(&#039;edit_shop_order&#039;, $order_id))
        return;

    $key = &#039;_shipping_phone&#039;; // 自訂欄位名稱

    // 確認表單有送出該欄位
    if (!isset($_POST[$key]))
        return;

    // 清理輸入資料，避免 XSS 或其他注入風險
    $value = wc_clean(wp_unslash($_POST[$key]));

    // 取得訂單物件
    $order = wc_get_order($order_id);
    if (!$order)
        return;

    // 使用 WC_Order API 更新訂單元資料
    $order-&gt;update_meta_data($key, $value);
    $order-&gt;save(); // 必須呼叫 save() 才會寫入資料庫
}, 50);</code></pre>
<h3 class="wp-block-heading">關鍵說明</h3>
<ul>
<li><code>current_user_can</code> 用來確保只有有編輯訂單權限的使用者能執行更新。</li>
<li><code>wc_clean</code> 和 <code>wp_unslash</code> 是 WordPress 與 WooCommerce 提供的安全函式，確保輸入資料安全。</li>
<li>使用 <code>update_meta_data</code> 與 <code>save</code> 是 WooCommerce 推薦的寫入方式，兼容新版 HPOS（高效訂單存儲系統）與舊版 postmeta。</li>
</ul>
<h2 class="wp-block-heading">實務應用與延伸</h2>
<p>此方法可擴充至任何自訂欄位，只要對應修改 <code>$key</code> 與表單名稱即可。實務中，還可搭配後台欄位輸入介面（如使用 <code>woocommerce_admin_order_data_after_billing_address</code> 鉤子）來完整實作自訂欄位的讀寫。</p>
<h2 class="wp-block-heading">常見問題與注意事項</h2>
<ul>
<li>忘記呼叫 <code>$order-&gt;save()</code> 將導致資料無法寫入。</li>
<li>欄位名稱建議加底線開頭避免與 WooCommerce 原生欄位衝突。</li>
<li>權限檢查不可省略，避免安全問題。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-php language-php php">&lt;?php
add_action(&#039;woocommerce_process_shop_order_meta&#039;, function ($order_id) {
    if (!current_user_can(&#039;edit_shop_order&#039;, $order_id))
        return;

    $key = &#039;_shipping_phone&#039;;

    if (!isset($_POST[$key]))
        return;

    $value = wc_clean(wp_unslash($_POST[$key]));

    $order = wc_get_order($order_id);
    if (!$order)
        return;

    $order-&gt;update_meta_data($key, $value);
    $order-&gt;save();
}, 50);</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/woocommerce-order-meta-save/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
