【问题标题】:Woocommerce Emails - Sending multiple emails based on product countWoocommerce 电子邮件 - 根据产品数量发送多封电子邮件
【发布时间】:2018-05-24 05:46:33
【问题描述】:

在 Woocommerce 中,我有一个使用 WC 字段来收集学生姓名和电子邮件地址的课程产品,以及在购买此课程产品时发送电子邮件的自定义电子邮件模板。截至目前,基于answer in this thread,我能够收集添加到这些自定义字段的电子邮件地址,并将一封电子邮件作为密件抄送收件人发送给每个学生。

我现在尝试让电子邮件在电子邮件正文中包含学生的姓名和电子邮件地址,但我不希望所有姓名都出现在同一封电子邮件中。例如,如果学生 A (studenta@example.com) 和学生 B (studentb@example.com) 都注册了同一个购买项目,那么学生 A 收到的电子邮件应该只在正文中包含类似“亲爱的学生 A,感谢您注册。使用您的电子邮件地址 studenta@example.com 登录。”学生 B 收到的邮件内容应该是一样的,但是有学生 B 的信息,他们应该看不到对方的信息。

因此,我将不得不发送多封电子邮件,具体取决于参与购买的学生人数(在订单元中设置,从所购买产品的元中添加)。

我尝试在 for() 之外添加一个 while() 以继续检查项目,直到它检查完所有项目,并在每次找到时再次发送,但我认为 foreach($items as $item) 结束了再次从第一个项目开始,因为它只向第一个收件人发送两封电子邮件。

***已更新**** 我将自定义电子邮件代码(已存储在插件中)更改为:

    $order = wc_get_order( $order_id );
            $items = $order->get_items(); 
            $course_ordered = false;
            $cqt = 1;
            $emailnum = 0;
        foreach ( $items as $item ) {

            $product_id = $item['product_id'];

            if ( $item['product_id']== 1164 ) {
                $course_ordered = true;
                $emailnum++;
                continue;
            }

        }

    if ( ! $course_ordered )
        return;
    while(  $cqt <= $emailnum ){
        // replace variables in the subject/headings
        $this->find[] = '{order_date}';
        $this->replace[] = date_i18n( woocommerce_date_format(), strtotime( $this->object->order_date ) );
        $this->find[] = '{order_number}';
        $this->replace[] = $this->object->get_order_number();
        if ( ! $this->is_enabled() || ! $this->get_recipient() )
            return;
        // woohoo, send the email!
        $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
        $cqt++;
    }

我的functions.php文件中的代码来自this answer,是:

add_filter( 'woocommerce_email_headers', 'student_email_notification', 20, 3 );
function student_email_notification( $header, $email_id, $order ) {
// Only for 'wc_course_order' notification
if( 'wc_course_order' != $email_id ) return $header; 

$student_emails = array();
//$enroll_num = 0;

// Loop though  Order IDs
foreach( $order->get_items() as $item_id => $item_data ){
    /*$course_qty = $item_data->get_quantity();
    $q = 1;
    while ( $q <= $course_qty){
        $enroll_num++;
        // Get the student full Name
        $full_name     = wc_get_order_item_meta( $item_id, 'First Name - '.$enroll_num, true );
        $full_name    .= ' ' . wc_get_order_item_meta( $item_id, 'Last Name - '.$enroll_num, true );
        // Get the student email
        $student_email = wc_get_order_item_meta( $item_id, 'Student Email - '.$enroll_num, true );
        if( ! empty($student_email) && $full_name != ' ' )
            // Format the name and the email and set it in an array
            $student_emails[] = utf8_decode($full_name . ' <' . $student_email . '>'); // Add name + email to the array
        $q++;
    }*/
        // Get the student full Name
        $full_name     = wc_get_order_item_meta( $item_id, 'First Name - 1', true );
        $full_name    .= ' ' . wc_get_order_item_meta( $item_id, 'Last Name - 1', true );
        // Get the student email
        $student_email = wc_get_order_item_meta( $item_id, 'Student Email - 1', true );
        if( ! empty($student_email) && $full_name != ' ' )
            // Format the name and the email and set it in an array
            $student_emails[] = utf8_decode($full_name . ' <' . $student_email . '>'); // Add name + 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
    $headers .= 'Cc: ' . implode(',', $student_emails) . "\r\n";
}
return $headers;
}

(如果被注释掉的部分未注释,它只发送给第一个收件人,而不是第二个,如果它作为两个单独的产品输入,如果我删除 while() 并改用它下面的部分,它会向所有收件人发送一封电子邮件) 它向所有收件人发送两次(即从所有自定义字段中获取所有电子邮件并将其用作抄送或密送,但我设置了它)两次,即向每个人发送两次相同的电子邮件。我怎样才能让它发送两次,但每次只发送给一个收件人?感谢您的任何建议!

【问题讨论】:

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


    【解决方案1】:

    我为我的问题的前半部分找到了解决方案 - 将产品元数据中的用户名和电子邮件添加到电子邮件的正文中 - 尽管仍然无法让它向每个电子邮件地址发送唯一的电子邮件(电子邮件地址来自此元):

    我将我的自定义电子邮件模板(这只是电子邮件模板,不是创建/发送电子邮件的自定义函数)更新为以下内容:

    <?php
    $student_emails = array();
    // Loop though  Order IDs
    foreach( $order->get_items() as $item_id => $item_data ){
        // Get the student full Name
            $full_name     = wc_get_order_item_meta( $item_id, 'First Name - 1', true );
            $full_name    .= ' ' . wc_get_order_item_meta( $item_id, 'Last Name - 1', true );
            // Get the student email
            $student_email = wc_get_order_item_meta( $item_id, 'Student Email - 1', true );
    
            print_r('Name: ' . $full_name . ', Login Email: ');
            print_r($student_email . '<br/>');
    
    }
    
    ?>
    

    这会在电子邮件正文中打印各种用户及其电子邮件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-03
      • 2011-02-08
      • 2016-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多