php - 向在 WooCommerce 中完成提交产品评论的用户发送电子邮件
问题描述
我正在使用以下代码,它工作正常。
function send_comment_email_notification( $comment_ID, $commentdata ) {
$comment = get_comment( $comment_id );
$postid = $comment->comment_post_ID;
$master_email = 'email@gmail.com';
var_dump($commentdata);
if( isset( $master_email ) && is_email( $master_email ) ) {
$message = 'New comment on <a href="' . get_permalink( $postid ) . '">' . get_the_title( $postid ) . '</a>';
add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
wp_mail( $master_email, 'New Comment', $message );
}
}
add_action( 'comment_post', 'send_comment_email_notification', 11, 2 );
但是,我想向刚刚在产品页面上评论的用户发送一封电子邮件,以便我可以向他们提供优惠券并感谢您给予评论。
问题是$master_email
变量,即“硬编码”。我需要捕获用户提交产品评论后输入的电子邮件
有什么建议吗?
解决方案
要获取用户提交产品评论后输入的电子邮件,您可以使用$commentdata['comment_author_email']
所以你得到:
function action_comment_post( $comment_ID, $comment_approved, $commentdata ) {
// Isset
if ( isset ( $commentdata['comment_author_email'] ) ) {
// Get author email
$author_email = $commentdata['comment_author_email'];
if ( is_email( $author_email ) ) {
// Post ID
$post_id = $commentdata['comment_post_ID'];
// Send e-mail
$to = $author_email;
$subject = 'The subject';
$body = sprintf( __(' Thank you for giving a review on %s', 'woocommerce' ), '<a href="' . get_permalink( $post_id ) . '">' . get_the_title( $post_id ) . '</a>' );
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
wp_mail( $to, $subject, $body, $headers );
}
}
}
add_action( 'comment_post', 'action_comment_post', 10, 3 );
推荐阅读
- python - 我想在 python 中打印我的排序列表,但它给了我一个错误
- javascript - 以嵌入模式检索 typeform 表单数据
- python - 无法在 ubuntu 18.04 中将 python 程序作为服务运行
- hash - Unhash Drupal 7 用户表密码
- javascript - 使用 jwt Bearer id_token 响应发布请求返回 401,但它在邮递员中使用相同的令牌
- algorithm - if 语句完全正确性的证明
- sql - 从搜索关键字过滤 SQL
- python - 防止梯度下降停止离局部最小值太远
- reactjs - React Admin 将子级添加到父级
- javascript - 过滤存储 cookie 的 JS 脚本