从WC Field Factory添加电子邮件收件人以获取woocommerce电子邮件通知 [英] Add email recipients from wc field factory for woocommerce email notification

查看:150
本文介绍了从WC Field Factory添加电子邮件收件人以获取woocommerce电子邮件通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用woocommerce销售课程产品。该课程将wc字段工厂用于学生姓名和学生电子邮件地址的自定义产品字段。学生电子邮件地址的字段名称是 student_email。然后,我尝试从该字段(电子邮件地址)中获取值,并将其用作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;

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;

这是在我的自定义电子邮件类php文件中完成的。但是似乎没有什么东西可以抓住student_email定制产品字段的值。任何帮助将不胜感激!

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!

推荐答案

代码已更新

在产品页面上设置了 student_email自定义字段后,该字段将以您为其设置的标签名称另存为订单项元数据(而不是订单元数据)…

因此,元键应为学生电子邮件 (标签名称),您将需要遍历订单项以获取那些电子邮件值(如果有)

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;
}

代码进入活动子主题(或活动主题)的function.php文件)。现在经过测试,可以正常工作。

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

这篇关于从WC Field Factory添加电子邮件收件人以获取woocommerce电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆