代码修改WooCommerce订单通知收件邮箱无效,仅发件人邮箱生效
问题解决方法
你的核心问题是用错了WooCommerce邮件收件人的选项名称,同时可能存在设置缓存未清理的情况,具体修复步骤如下:
1. 修正选项名称(关键)
WooCommerce中订单通知收件人的选项名后缀是_recipient,而非你代码里的_email。正确的选项名如下:
- 新订单收件人:
woocommerce_new_order_recipient - 取消订单收件人:
woocommerce_cancelled_order_recipient - 失败订单收件人:
woocommerce_failed_order_recipient
你的代码里错误地使用了woocommerce_new_order_email这类名称,虽然日志显示wp_options字段被更新,但这些字段并非WooCommerce实际读取的收件人配置项。
2. 修正后的完整代码
function change_notification_email_callback() { // 调试日志:打印表单数据 error_log( print_r( $_POST, true ) ); if ( isset( $_POST['new_notification_email'] ) && ! empty( $_POST['new_notification_email'] ) ) { $new_email = sanitize_email( $_POST['new_notification_email'] ); // 更新邮件配置 update_option( 'woocommerce_email_from_address', $new_email ); // 发件人邮箱(原代码正确) update_option( 'woocommerce_new_order_recipient', $new_email ); // 新订单收件人(修正选项名) update_option( 'woocommerce_cancelled_order_recipient', $new_email ); // 取消订单收件人(修正选项名) update_option( 'woocommerce_failed_order_recipient', $new_email ); // 失败订单收件人(修正选项名) // 调试日志:验证更新后的值 error_log( 'Updated New Order Recipient: ' . get_option( 'woocommerce_new_order_recipient' ) ); error_log( 'Updated Cancelled Order Recipient: ' . get_option( 'woocommerce_cancelled_order_recipient' ) ); error_log( 'Updated Failed Order Recipient: ' . get_option( 'woocommerce_failed_order_recipient' ) ); $message = '通知邮箱修改成功。'; $notice_type = 'success'; } else { $message = '修改失败,请输入有效的邮箱地址。'; $notice_type = 'error'; } // 显示后台通知 add_flash_notice( $message, $notice_type ); // 重定向回WooCommerce仪表盘 wp_redirect( admin_url( 'admin.php?page=wc-dashboard' ) ); exit; }
3. 额外处理:清理WooCommerce缓存
如果修正代码后后台仍显示旧邮箱,可能是WooCommerce缓存了设置值,需要:
- 按Ctrl+F5强制刷新WooCommerce设置页面
- 清理WordPress后台缓存(若使用缓存插件)
- 重启PHP服务(针对服务器级缓存场景)
4. 验证测试
- 提交新邮箱后,进入WooCommerce→设置→邮件页面,确认各订单通知的收件人已更新
- 使用WooCommerce测试支付网关创建测试订单,检查通知邮件是否发送到新邮箱
内容的提问来源于stack exchange,提问作者DisplayName
相关产品推荐
相关产品推荐

