[WooCommerce] 程式化觸發訂單狀態發信機制

本篇文章更新時間:2019/05/25
如有資訊過時或語誤之處,歡迎使用 Contact 功能通知。
一介資男的 LINE 社群開站囉!歡迎入群聊聊~
如果本站內容對你有幫助,歡迎使用 BFX Pay 加密貨幣新台幣 贊助支持。


內建訂單如果在介面操作下改訂單狀態會觸發一系列對應的操作。像是取消訂單會發信通知管理員,然後把庫存補回商品等等的系統行為。

但如果是被程式化方式給改了狀態,後續有事件需要被連動觸發該怎麼處理?

今天的案例是使用外掛 WooCommerce Cancel Abandoned Order 檢查訂單狀態是否過期,並且設定取消狀態。

但是,它並沒有去觸發後續的反映,像是訂單取消信件這個行為。

好在這開發者有留了不少 Hooks 可以接,所以這個訂單取消的發信客製化功能程式碼範例如下:

function mxp_woo_cao_cancel_order_event($order_id){
        $mailer = WC()->mailer();
        $mails = $mailer->get_emails();
        if ( ! empty( $mails ) ) {
                foreach ( $mails as $mail ) {
                        if ( $mail->id == 'cancelled_order' ) {
                           $mail->trigger( $order_id );
                        }
                }
        }
}
add_action('woo_cao_cancel_order','mxp_woo_cao_cancel_order_event',11,1);

Gist: Link

關鍵是方法內的那段程式,指定發送信件類別後,帶入訂單編號去觸發發信機制。

Ref: How to trigger WooCommerce order complete email?


Share:

作者: Chun

資訊愛好人士。主張「人人都該為了偷懶而進步」。期許自己成為斜槓到變進度條 100% 的年輕人。[///////////____36%_________]

參與討論

5 則留言

  1. 自動引用通知: [WooCommerce] 針對付款方式設定取消訂單功能 | 一介資男
  2. .
    不知道這兩篇有沒有幫助:

    1.
    https://docs.woocommerce.com/document/email-faq/
    >> Pending orders are orders where the customer clicked “Place Order” but abandoned the payment page (depending on the payment gateway, i.e., PayPal) or had their credit card transaction declined.

    2.
    https://github.com/woocommerce/woocommerce/issues/15707
    >> A pending -> cancelled order means nothing.

    所以我推測應該是WC對pending order的處理方式,而不是那個外掛的問題(我沒用過那套外掛)。

    所以如果pending -> cancelled 不會寄信。(沒測過,只確定pending不會寄信)
    當 on-hold -> cancelled 才會寄信。(確定會寄信)

  3. .
    不知道這兩篇有沒有幫助:

    1.
    https://docs.woocommerce.com/document/email-faq/
    >> Pending orders are orders where the customer clicked “Place Order” but abandoned the payment page (depending on the payment gateway, i.e., PayPal) or had their credit card transaction declined.

    2.
    https://github.com/woocommerce/woocommerce/issues/15707
    >> A pending -> cancelled order means nothing.

    所以我推測應該是WC對pending order的處理方式,而不是那個外掛的問題(我沒用過那套外掛)。

    所以:

    當pending -> cancelled 不會寄信。(沒測過,只確定pending不會寄信)

    當 on-hold -> cancelled 才會寄信。(確定會寄信)

  4. 嗯嗯,其實起源是因為庫存管理那邊超過時間設定居然不會觸發取消訂單(很奇怪),而且追查庫存那邊的寫法也沒有比較特別,就是針對 pending 來處理改狀態。如此無解我才去改套這款,沒想到他會改狀態但也不會觸發信件,看程式也是正規寫法,超詭異XD 只好自己來觸發了~~

  5. Jack Wu 真的還好有 hook,所以這款彈性不錯!可以把金流端的 timeout 去掉,統一用這個做法來管理訂單狀態。(可以個別針對金流方法設定過期時間週期)

發佈留言

發佈回覆給「一介資男」的留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *


文章
Filter
Apply Filters
Mastodon