You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress/WooCommerce中COD支付订单On Hold状态邮件未触发求助

Fix: COD Orders Not Triggering On-Hold Email in WooCommerce 3.3.5

Hey there, let's sort out why your COD orders aren't sending the "Order On Hold" email even though the status is updating correctly. Here's the breakdown and fix:

The Root Issue

Your current code sets the order status to on-hold directly, but WooCommerce's email system relies on status transition hooks (like woocommerce_order_status_pending_to_on-hold_notification) to trigger emails. Two gaps are causing the problem:

  1. The woocommerce_thankyou action might run after default status processing, so the transition hook doesn't fire as expected.
  2. Your existing email hook function only handles pending_to_processing transitions, not the pending_to_on-hold shift that COD orders use.

Revised Code to Fix the Email Trigger

Here's updated code that preserves your status logic while ensuring the On-Hold email fires for COD, BACS, and Cheque orders:

add_action( 'woocommerce_thankyou', 'wc_auto_complete_paid_order', 20, 1 );
function wc_auto_complete_paid_order( $order_id ) {
    if ( ! $order_id ) return;

    $order = wc_get_order( $order_id );
    $current_status = $order->get_status();

    // Handle BACS, COD, Cheque: Set to On Hold and ensure email triggers
    if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque' ) ) ) {
        // Avoid redundant status updates
        if ( $current_status !== 'on-hold' ) {
            // Update status with transition hooks enabled (default behavior)
            $order->update_status( 'on-hold', 'Awaiting payment confirmation' );
            
            // Fallback: Manually trigger the On-Hold email if hook didn't fire
            if ( class_exists( 'WC_Email_Customer_On_Hold_Order' ) ) {
                $email = new WC_Email_Customer_On_Hold_Order();
                $email->trigger( $order_id );
            }
        }
    } 
    // Handle PayPal & other paid methods: Set to Completed
    else {
        if ( $current_status !== 'completed' ) {
            $order->update_status( 'completed', 'Payment automatically confirmed' );
        }
    }
}

function adjust_woocommerce_email_triggers( $email_class ) {
    // Keep your existing logic: Replace Processing email with On-Hold for pending->processing
    remove_action( 'woocommerce_order_status_pending_to_processing_notification', array( $email_class->emails['WC_Email_Customer_Processing_Order'], 'trigger' ) );
    add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $email_class->emails['WC_Email_Customer_On_Hold_Order'], 'trigger' ) );
    
    // Critical addition: Map pending->on-hold transition to On-Hold email
    add_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $email_class->emails['WC_Email_Customer_On_Hold_Order'], 'trigger' ) );
}
add_action( 'woocommerce_email', 'adjust_woocommerce_email_triggers' );

Key Improvements:

  • Manual Email Fallback: If the status transition hook fails to fire (common on the thank-you page), we manually instantiate and trigger the On-Hold email.
  • Status Checks: Prevents redundant status updates that could cause loops or duplicate actions.
  • Explicit Email Mapping: Adds the pending_to_on-hold hook to ensure COD/BACS orders trigger the correct email when their status changes.
  • Admin Notes: Optional status notes help track why an order was updated, making debugging easier.

Testing Steps:

  1. Place a test order using COD payment.
  2. Confirm the order status is set to "On Hold" in WooCommerce Admin.
  3. Check the customer's inbox (and spam folder) for the On-Hold email.
  4. If issues persist, check WooCommerce > Status > Logs for email-related errors.

内容的提问来源于stack exchange,提问作者Fjott

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:46:04