<?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/%E4%BA%A4%E6%98%93%E5%AE%8C%E6%88%90/feed/" rel="self" type="application/rss+xml" />
	<link>https://piglife.tw</link>
	<description>Hello World，一個紀錄生活與學習的地方</description>
	<lastBuildDate>Sat, 20 Dec 2025 03:46:29 +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>使用 MemberPress 交易完成鉤子自動寄送訂閱確認信</title>
		<link>https://piglife.tw/technical-notes/memberpress-transaction-email/</link>
					<comments>https://piglife.tw/technical-notes/memberpress-transaction-email/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Sat, 20 Dec 2025 03:46:29 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[MemberPress]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp_mail]]></category>
		<category><![CDATA[交易完成]]></category>
		<category><![CDATA[自動寄信]]></category>
		<category><![CDATA[訂閱通知]]></category>
		<guid isPermaLink="false">https://piglife.tw/technical-notes/memberpress-transaction-email/</guid>

					<description><![CDATA[介紹如何利用 MemberPress 交易完成事件自動寄送訂閱方案付款成功的確認信，包含交易資料判斷...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>這段程式碼示範如何在 WordPress 使用 MemberPress 交易完成事件鉤子，自動寄送訂閱方案的確認電子郵件。適合使用 MemberPress 管理會員訂閱，並希望自動化寄送付款成功通知的工程師或自學者。</p>
<h2 class="wp-block-heading">交易完成事件監聽</h2>
<p>透過 <code>add_action</code> 監聽 <code>mepr-event-transaction-completed</code> 事件，當交易完成時觸發自訂函式。函式內部先檢查事件與交易物件有效性，避免錯誤執行。</p>
<pre><code class="lang-php language-php php">add_action(&#039;mepr-event-transaction-completed&#039;, function($event) {
  if(!is_object($event)) return;

  $txn = $event-&gt;get_data();
  if(!is_object($txn)) return;
  // ...
});</code></pre>
<h2 class="wp-block-heading">交易資料擷取與條件判斷</h2>
<p>取得交易相關資訊如使用者 ID、方案 ID、交易 ID、付款狀態與金額，並做以下條件過濾：</p>
<ul>
<li>使用者、方案、交易 ID 必須有效</li>
<li>交易狀態必須為 <code>complete</code></li>
<li>可選擇只寄首次付款通知（此範例預設為每次付款皆寄）</li>
<li>防止重複寄送，透過交易文章 meta 標記</li>
</ul>
<p>這些判斷確保只有有效且未重複的交易會觸發郵件寄送。</p>
<h2 class="wp-block-heading">根據方案選擇郵件模板</h2>
<p>根據方案 ID（範例中為 626 月付方案與 639 年付方案）選擇對應的郵件主旨與 HTML 內容。郵件內容包含隱藏文字供郵件摘要使用，以及簡潔的訂閱資訊與操作連結。</p>
<pre><code class="lang-php language-php php">if($membership_id === 626) {
  $subject = $subject_626;
  $message = $message_626;
} elseif($membership_id === 639) {
  $subject = $subject_639;
  $message = $message_639;
} else {
  return;
}</code></pre>
<h2 class="wp-block-heading">寄送郵件與防重複標記</h2>
<p>使用 WordPress 內建的 <code>wp_mail</code> 函式寄送 HTML 格式郵件，並在成功寄出後，透過 <code>update_post_meta</code> 設定交易文章的自訂 meta，避免重複寄信。</p>
<pre><code class="lang-php language-php php">$headers = [&#039;Content-Type: text/html; charset=UTF-8&#039;];
$sent = wp_mail($user-&gt;user_email, $subject, $message, $headers);

if($sent) {
  update_post_meta($txn_id, &#039;_custom_plan_email_sent&#039;, &#039;yes&#039;);
}</code></pre>
<h2 class="wp-block-heading">實務應用與優化建議</h2>
<ul>
<li>可依照不同方案擴充更多模板，提升客製化體驗</li>
<li>建議搭配會員資料完整性檢查，確保郵件內容正確</li>
<li>若有多語系需求，可整合翻譯函式動態產生郵件內容</li>
<li>透過設定參數控制是否只寄首次付款通知，靈活調整行銷策略</li>
</ul>
<h2 class="wp-block-heading">常見問題與注意事項</h2>
<ul>
<li>確認 MemberPress 交易狀態名稱是否與程式碼一致，避免漏寄</li>
<li>郵件內容中圖片 URL 請替換成實際可用路徑</li>
<li>測試時注意交易狀態與交易金額，避免誤寄測試郵件</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-php language-php php">&lt;?php
add_action(&#039;mepr-event-transaction-completed&#039;, function($event) {
  if(!is_object($event)) return;

  // true = 只寄首次；false = 每次扣款都寄
  $SEND_FIRST_PAYMENT_ONLY = false;

  $txn = $event-&gt;get_data();
  if(!is_object($txn)) return;

  // 交易基本資訊
  $user_id       = intval($txn-&gt;user_id ?? 0);
  $membership_id = intval($txn-&gt;product_id ?? 0);
  $txn_id        = intval($txn-&gt;id ?? 0); // MemberPress 交易文章 ID
  $total         = floatval($txn-&gt;total ?? 0);
  $status        = strval($txn-&gt;status ?? &#039;&#039;);

  if($user_id &lt;= 0 || $membership_id &lt;= 0 || $txn_id &lt;= 0) return;

  // 僅完成交易才寄（雙保險）
  if($status !== &#039;complete&#039;) return;

  // 避免 $0 交易（試用 / 全額折扣）也發信
//   if($total &lt;= 0) return;

  // 只寄首次付款？
  if($SEND_FIRST_PAYMENT_ONLY === true &amp;&amp; method_exists($txn,&#039;is_first_payment&#039;) &amp;&amp; !$txn-&gt;is_first_payment()) {
    return;
  }

  // 防重複寄送：用交易 ID 做旗標
  if(get_post_meta($txn_id, &#039;_custom_plan_email_sent&#039;, true) === &#039;yes&#039;) {
    return;
  }

  $user = get_user_by(&#039;id&#039;, $user_id);
  if(!$user || empty($user-&gt;user_email)) return;

  $product_name = get_the_title($membership_id);

  // ===== 月付 626 =====
  $subject_626 = &#039;感謝您購買 &#039;.$product_name;
  $message_626 = &lt;&lt;&lt;HTML
&lt;div style=&quot;display: none; overflow: hidden; line-height: 1px; opacity: 0; max-height: 0; max-width: 0;&quot;&gt;你已成功訂閱 。你的訂閱方案將會自動續訂：月付 $129，可隨時取消。&lt;/div&gt;
&lt;table style=&quot;width: 100%; background: #f5f6f8; margin: 0; padding: 0;&quot; role=&quot;presentation&quot;&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;
&lt;table style=&quot;width: 600px; margin: 0 auto;&quot; role=&quot;presentation&quot;&gt;
&lt;tr&gt;&lt;td style=&quot;padding: 0px 24px; background: #ffffff;&quot; align=&quot;left&quot;&gt;
&lt;img src=&quot;&quot; width=&quot;200&quot; /&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td style=&quot;padding: 32px 24px; background: #ffffff; font-family: Helvetica, Arial, &#039;PingFang TC&#039;, &#039;Microsoft JhengHei&#039;, sans-serif; color: #111827;&quot;&gt;
&lt;table role=&quot;presentation&quot; width=&quot;100%&quot;&gt;
&lt;tr&gt;&lt;td valign=&quot;top&quot; width=&quot;6&quot;&gt;
&lt;div style=&quot;width: 3px; height: 22px; background: #39A297; border-radius: 2px; margin-top: 6px;&quot;&gt;&lt;/div&gt;
&lt;/td&gt;
&lt;td style=&quot;padding-left: 10px;&quot;&gt;
&lt;p style=&quot;font-size: 16px; line-height: 1.75; margin: 0;&quot;&gt;你已成功訂閱 &lt;strong&gt;&lt;/strong&gt; 。&lt;/p&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;div style=&quot;height: 16px;&quot;&gt;&lt;/div&gt;
&lt;p style=&quot;font-size: 16px; line-height: 1.75; margin: 0;&quot;&gt;你的訂閱方案將會自動續訂：&lt;strong&gt;月付 $129&lt;/strong&gt;&lt;br&gt;你可以隨時取消。&lt;/p&gt;

&lt;div style=&quot;height: 16px;&quot;&gt;&lt;/div&gt;
&lt;a style=&quot;background: #39A297; border-radius: 8px; padding: 12px 18px; display: inline-block; color: #ffffff; text-decoration: none;&quot; href=&quot;https://.com/account/&quot; target=&quot;_blank&quot;&gt;點擊這裡登入&lt;/a&gt;

&lt;div style=&quot;height: 28px;&quot;&gt;&lt;/div&gt;
&lt;p style=&quot;font-size: 14px; color: #6b7280;&quot;&gt;如有任何問題，請於聯絡我們留言你的需求。&lt;/p&gt;
&lt;p style=&quot;font-size: 16px;&quot;&gt; 團隊&lt;/p&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
HTML;

  // ===== 年付 639 =====
  $subject_639 = &#039;感謝您購買 &#039;.$product_name;
  $message_639 = &lt;&lt;&lt;HTML
&lt;div style=&quot;display: none;overflow: hidden;line-height: 1px;opacity: 0;max-height: 0;max-width: 0&quot;&gt;你已成功訂閱 。你的訂閱方案將會自動續訂：年付 $1,290，可隨時取消。&lt;/div&gt;
&lt;table style=&quot;width: 100%;background: #f5f6f8;margin: 0;padding: 0&quot; role=&quot;presentation&quot;&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;
&lt;table style=&quot;width: 600px;margin: 0 auto&quot; role=&quot;presentation&quot;&gt;
&lt;tr&gt;&lt;td style=&quot;padding: 0 24px;background: #ffffff&quot; align=&quot;left&quot;&gt;
&lt;img src=&quot;https://.com/wp-content/uploads/2025/10/forGoogleLogo.png&quot; width=&quot;200&quot; /&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td style=&quot;padding: 32px 24px;background: #ffffff;font-family: Helvetica, Arial, &#039;PingFang TC&#039;, &#039;Microsoft JhengHei&#039;, sans-serif;color: #111827&quot;&gt;
&lt;table role=&quot;presentation&quot; width=&quot;100%&quot;&gt;&lt;tr&gt;&lt;td valign=&quot;top&quot; width=&quot;6&quot;&gt;
&lt;div style=&quot;width: 3px;height: 22px;background: #16a34a;border-radius: 2px;margin-top: 6px&quot;&gt;&lt;/div&gt;
&lt;/td&gt;
&lt;td style=&quot;padding-left: 10px&quot;&gt;
&lt;p style=&quot;font-size: 16px;line-height: 1.75;margin: 0&quot;&gt;你已成功訂閱 &lt;strong&gt;&lt;/strong&gt; 。&lt;/p&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;div style=&quot;height: 16px&quot;&gt;&lt;/div&gt;
&lt;p style=&quot;font-size: 16px;line-height: 1.75;margin: 0&quot;&gt;你的訂閱方案將會自動續訂：&lt;strong&gt;年付 $1,290&lt;/strong&gt;
&lt;br /&gt;你可以隨時取消。&lt;/p&gt;

&lt;div style=&quot;height: 16px&quot;&gt;&lt;/div&gt;
&lt;p style=&quot;font-size: 16px;line-height: 1.75;margin: 0&quot;&gt;別忘了到會員資訊裡留下你的收件資料，&lt;br /&gt;讓我們能夠把贈品正確地寄送到你手上。&lt;/p&gt;

&lt;div style=&quot;height: 16px&quot;&gt;&lt;/div&gt;
&lt;a style=&quot;background: #39A297;border-radius: 8px;padding: 12px 18px;display: inline-block;color: #ffffff;text-decoration: none&quot; href=&quot;#&quot; target=&quot;_blank&quot;&gt;設定你的收件資料&lt;/a&gt;

&lt;div style=&quot;height: 28px&quot;&gt;&lt;/div&gt;
&lt;p style=&quot;font-size: 14px;color: #6b7280;&quot;&gt;如有任何問題，請於聯絡我們留言你的需求。&lt;/p&gt;
&lt;p style=&quot;font-size: 16px;&quot;&gt; 團隊&lt;/p&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
HTML;

  // 套用對應模板
  if($membership_id === 626) {
    $subject = $subject_626;
    $message = $message_626;
  } elseif($membership_id === 639) {
    $subject = $subject_639;
    $message = $message_639;
  } else {
    return;
  }

  // 寄送
  $headers = [&#039;Content-Type: text/html; charset=UTF-8&#039;];
  $sent = wp_mail($user-&gt;user_email, $subject, $message, $headers);

  // 設定防重複旗標（只有寄成功才標記）
  if($sent) {
    update_post_meta($txn_id, &#039;_custom_plan_email_sent&#039;, &#039;yes&#039;);
  }
});</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/memberpress-transaction-email/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
