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!
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:
Code goes in function.php file of your active child theme (or active theme). Now Tested and works.