[WordPress] WooCommerce 訂單取消或失敗發信通知客戶

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


預設 WooCommerce 這個取消訂單與付款失敗的信是發給指定的管理員信箱。

如果需要順便通知客戶,就要動一點手腳。

add_filter( 'woocommerce_email_recipient_cancelled_order', 'wc_cancelled_order_add_customer_email', 10, 2 );
add_filter( 'woocommerce_email_recipient_failed_order', 'wc_cancelled_order_add_customer_email', 10, 2 );
function wc_cancelled_order_add_customer_email( $recipient, $order ){
    // Avoiding errors in backend (mandatory when using $order argument)
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
    return $recipient .= "," . $order->get_billing_email();
}

Ref: Send cancelled and failed order email to customer in Woocommerce 3

Gist: Link

放此程式碼片段進主題 functions.php 中即可。

如果因為訂單失敗要做一些判斷,可以從 wp_head 下手。

function remove_tracking_from_failed_orders($order_id) {
    global $wp;
    // 確保當前執行是在訂單金流返回頁面,不是就離開
    if (!is_wc_endpoint_url('order-received')) {
        return;
    }
    // 從 URL 取得訂單 ID
    $order_id = absint($wp->query_vars[get_option('woocommerce_checkout_order_received_endpoint', 'order-received')]);
    $order = wc_get_order($order_id);
    if ($order->has_status('failed')) {
        // 訂單狀態是失敗的話,把感謝頁的所有串接 Hook 拔掉
        remove_all_actions('woocommerce_thankyou');
    }
}
add_action('wp_head', 'remove_tracking_from_failed_orders');

意思是趕在 wp_head 階段先把後續的事件作調整,就可以避免觸發,或是改觸發。


Share:

作者: Chun

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

發佈留言

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


文章
Filter
Apply Filters
Mastodon