简体   繁体   English

添加来自 wc 现场工厂的电子邮件收件人以进行 woocommerce 电子邮件通知

[英]Add email recipients from wc field factory for woocommerce email notification

I am selling a course product using woocommerce.我正在使用 woocommerce 销售课程产品。 The course uses wc fields factory for custom product fields for student name and student email address.该课程使用 wc 字段工厂作为学生姓名和学生电子邮件地址的自定义产品字段。 The field name for student email address is "student_email".学生电子邮件地址的字段名称是“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.然后,我尝试从该字段(电子邮件地址)中获取值,并在购买此产品时将其用作来自 woocommerce 的电子邮件的收件人。

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.这是在我的自定义电子邮件类 php 文件中完成的。 But nothing seems to grab the values of the student_email custom product field.但似乎没有什么能获取 student_email 自定义产品字段的值。 Any help would be much appreciated!任何帮助将不胜感激!

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…由于您的“student_email”自定义字段是在产品页面上设置的,因此它会以您为其设置的标签名称保存为订单项元数据(而不是订单元数据)...
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 below 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).代码位于活动子主题(或活动主题)的 function.php 文件中。 Now Tested and works.现在测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将电子邮件收件人添加到 Woocommerce 中本地取货的新订单通知 - Add email recipients to New order notification for Local pickup in Woocommerce 将产品自定义字段中的收件人添加到Woocommerce新订单电子邮件通知中 - Add recipients from product custom fields to Woocommerce new order email notification 基于 Woocommerce 新订单通知的配送方式 ID 的电子邮件收件人 - Email recipients based on Shipping method Id for Woocommerce new order notification 基于 WooCommerce 电子邮件通知中销售的产品的不同收件人 - Different recipients based on products sold in WooCommerce email notification WooCommerce 电子邮件通知中基于产品类别的不同收件人 - Different recipients based on product category in WooCommerce email notification 如何从 WooCommerce 中的新订单电子邮件中获取电子邮件收件人 - How to get email recipients from new order email in WooCommerce 在Woocommerce WC_Email类中添加我的if语句 - Add my if statement in Woocommerce WC_Email class 如何在Woocommerce中添加自定义电子邮件通知? - How to add custom email notification in Woocommerce? 将客户电子邮件地址添加到WooCommerce中的新帐户电子邮件通知 - Add customer email address to new account email notification in WooCommerce 在Woocommerce结帐上添加运送电子邮件并发送电子邮件通知 - Add Shipping Email On Woocommerce Checkout And Send Email Notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM