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

Woocommerce订单完成后向用户元数据邮箱发额外邮件问题排查

Got it, let's fix this issue for you! The core problem with your original code is that you're trying to fetch user-specific metadata using get_post_meta()—which pulls data tied to the order post—while iconic-register-email is stored in the wp_usermeta table, linked to the customer's user account, not the order itself.

Here's the corrected, dynamic solution that pulls the target email directly from the user's metadata without hardcoding any addresses:

add_filter( 'woocommerce_email_recipient_new_order', 'add_custom_user_email_to_new_order_recipients', 10, 2 );
function add_custom_user_email_to_new_order_recipients( $recipient, $order ) {
    // Validate the order object first to avoid errors
    if ( ! is_a( $order, 'WC_Order' ) ) {
        return $recipient;
    }

    // Get the ID of the user associated with the order
    $user_id = $order->get_user_id();

    // Only proceed if the order is linked to a registered user (guest orders won't have a user ID)
    if ( $user_id > 0 ) {
        // Fetch the custom email from the user's metadata
        $custom_email = get_user_meta( $user_id, 'iconic-register-email', true );

        // Add the email to recipients only if it's a valid, non-empty address
        if ( ! empty( $custom_email ) && is_email( $custom_email ) ) {
            // Prevent duplicate entries in the recipient list
            if ( strpos( $recipient, $custom_email ) === false ) {
                $recipient .= ', ' . $custom_email;
            }
        }
    }

    return $recipient;
}

Key improvements over your original code:

  • Uses get_user_meta() instead of get_post_meta() to access the wp_usermeta table where your target email is stored
  • Validates the order object to avoid fatal errors in edge cases
  • Checks if the order is tied to a registered user (guest orders won't have a user ID to pull metadata from)
  • Verifies the fetched value is a valid email address before adding it
  • Prevents duplicate email entries in the recipient list

Replace your existing code in the child theme's functions.php with this snippet, and it should correctly add the user's iconic-register-email address to the new order email recipients whenever a registered customer completes an order.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:58:45