如何在WooCommerce中同时向账单邮箱与用户资料邮箱发送订单通知?
解决WooCommerce后台创建订单时同时发送邮件至用户资料邮箱和账单邮箱的问题
你可以通过WooCommerce的邮件收件人过滤钩子,将用户的资料邮箱添加到订单邮件的收件人列表中,具体实现如下:
代码实现
添加以下PHP代码到你的WordPress主题functions.php文件,或者使用自定义代码管理插件(如Code Snippets)来导入:
// 针对新订单邮件添加用户资料邮箱 add_filter( 'woocommerce_email_recipient_new_order', 'add_user_profile_email_to_order_notifications', 10, 2 ); // 针对用户处理中订单邮件添加用户资料邮箱 add_filter( 'woocommerce_email_recipient_customer_processing_order', 'add_user_profile_email_to_order_notifications', 10, 2 ); // 针对用户已完成订单邮件添加用户资料邮箱 add_filter( 'woocommerce_email_recipient_customer_completed_order', 'add_user_profile_email_to_order_notifications', 10, 2 ); function add_user_profile_email_to_order_notifications( $recipient, $order ) { // 校验订单对象有效性及关联用户ID if ( ! is_a( $order, 'WC_Order' ) || ! $order->get_user_id() ) { return $recipient; } // 获取对应用户对象 $user = get_user_by( 'id', $order->get_user_id() ); if ( ! $user ) { return $recipient; } $user_profile_email = $user->user_email; $billing_email = $order->get_billing_email(); // 仅当两个邮箱不同时添加,避免重复 if ( $user_profile_email !== $billing_email && strpos( $recipient, $user_profile_email ) === false ) { $recipient .= ',' . $user_profile_email; } return $recipient; }
说明
- 上述代码会自动对新订单、处理中订单、已完成订单这三类核心订单邮件生效,确保同时发送至账单邮箱和用户资料邮箱。
- 如果你需要覆盖更多订单相关邮件(如订单暂停、退款通知等),只需复制对应的
add_filter行,替换钩子名称中的邮件ID即可(例如woocommerce_email_recipient_customer_on_hold_order对应订单暂停通知)。 - 建议使用自定义代码插件添加代码,避免主题更新时丢失修改。
内容的提问来源于stack exchange,提问作者Igor Boldyrev
相关产品推荐
相关产品推荐

