Add email recipients from wc field factory for woo

2019-08-14 22:22发布

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条回答
相关推荐>>
2楼-- · 2019-08-14 22:53

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.

查看更多
登录 后发表回答