dongsu3138 2018-04-05 01:01
浏览 99
已采纳

从wc field factory添加电子邮件收件人以获取woocommerce电子邮件通知

I am selling a course product using woocommerce. The course uses wc fields factory for custom product fields for student name and student email address. The field name for student email address is "student_email". I am then trying to take the value from that field (the email address) and use that as the recipient for an email from woocommerce upon the purchase of this product.

The values inputted do show up on the cart page, the order receipt emails, etc. The custom email template I set up does work (it currently sends to the admin email until I get this to work). But I can't figure out how to grab the student email address value to use as the recipient.

I've tried several things, including the following:

$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
// Get "student email" custom field value
$student_emails = get_post_meta($order_id, "wccpf_student_email", true );
$this->recipient = $student_emails;

and

function custom_add_to_cart_action_handler($_cart_item_data, $_product_id) {
if(isset($_cart_item_data[“wccpf_student_email”])) {
$value = $_cart_item_data[“wccpf_student_email”];
return $value;
}
}
add_filter(‘woocommerce_add_cart_item_data’, array( $this, ‘custom_add_to_cart_action_handler’ ), 100, 2);
$this->recipient = $value;

This is done within my custom email class php file. But nothing seems to grab the values of the student_email custom product field. Any help would be much appreciated!

  • 写回答

1条回答 默认 最新

  • douchun1859 2018-04-05 02:12
    关注

    Code updated

    As your "student_email" custom fields is set in on the product page, it's saved in as order items meta data (and not order meta data) with the label name that you have set for it…
    So the meta key should be "Student email" (the label name) and you will need to loop through order items to get those email values (if there is more than one Item for the order.

    The code bellow will get those emails (if they exist) and will add theme to email recipients for order "processing" and "completed" email notifications:

    add_filter( 'woocommerce_email_recipient_customer_processing_order', 'student_email_notification', 10, 2 );
    add_filter( 'woocommerce_email_recipient_customer_completed_order', 'student_email_notification', 10, 2 );
    function student_email_notification( $recipient, $order ) {
        if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
    
        $student_emails = array();
    
        // Loop though  Order IDs
        foreach( $order->get_items() as $item_id => $item ){
            // Get the student email
            $student_email = wc_get_order_item_meta( $item_id, 'Student email', true );
            if( ! empty($student_email) )
                $student_emails[] = $student_email; // Add email to the array
        }
    
        // If any student email exist we add it
        if( count($student_emails) > 0 ){
            // Remove duplicates (if there is any)
            $student_emails = array_unique($student_emails);
            // Add the emails to existing recipients
            $recipient .= ',' . implode( ',', $student_emails );
        }
        return $recipient;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Now Tested and works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化