本篇文章更新時間:2019/05/10
如有資訊過時或語誤之處,歡迎使用 Contact 功能通知或向一介資男的 LINE 社群反應。
如果本站內容對你有幫助,歡迎贊助支持


預設 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

WordPress 社群貢獻者、開源社群推廣者。專注於 WordPress 外掛開發、網站效能最佳化、伺服器管理,以及 iDempiere 開源 ERP 導入與客製開發。曾參與 WordCamp Taipei 等社群活動,GitHub Arctic Code Vault Contributor。提供資訊顧問、WordPress 開發教學、主機最佳化與企業 ERP 整合服務。

發佈留言

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


文章
Filter
Mastodon