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

时间:2018-04-05 01:01:31

标签: php wordpress woocommerce custom-fields email-notifications

我正在使用woocommerce销售课程产品。该课程使用wc字段工厂为学生姓名和学生电子邮件地址定制产品字段。学生电子邮件地址的字段名称为" student_email"。然后,我尝试从该字段中获取值(电子邮件地址),并在购买此产品时将其用作woocommerce的电子邮件的收件人。

输入的值确实显示在购物车页面,订单收据电子邮件等上。我设置的自定义电子邮件模板确实有效(它目前发送到管理员电子邮件,直到我开始工作)。但我无法弄清楚如何获取学生电子邮件地址值以用作收件人。

我尝试了几件事,包括以下内容:

$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自定义产品字段的值。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

代码已更新

作为你的" student_email"自定义字段在产品页面上设置,它作为订单商品元数据(而不是订购元数据)保存为您为其设置的标签名称...
因此,元键应该是"学生电子邮件" (标签名称),您需要循环浏览订单商品以获取这些电子邮件值(如果订单中有多个商品。

以下代码将获取这些电子邮件(如果存在)并将为电子邮件收件人添加主题以便订购"处理"和#34;完成"电子邮件通知:

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文件中。现在经过测试并且有效。

相关问题