we have used Checkout Field Editor for WooCommerce where we have created extra email field and we tried to send mail to the email that email field value after every new order and here is the code:
function custom_email_for_gift( $order_id ) {
global $wpdb;
$order = wc_get_order( $order_id );
//this is the field we were talking about
$gift_to_person_email =$wpdb->get_var(
"SELECT meta_value FROM wp_postmeta WHERE post_id=$order_id AND meta_key='gift_to_person_email'"
) ?: '';
$gift_to_organization_email =$mailfororginazation=$wpdb->get_var(
"SELECT meta_value FROM wp_postmeta WHERE post_id=$order_id AND meta_key='gift_to_organization_email'"
) ?: '';
$name = $order->get_billing_first_name();
if($gift_to_person_email){
$to = $gift_to_person_email;
}elseif($gift_to_organization_email){
$to = $gift_to_organization_email;
}
if($to){
$subject = 'The subject';
$headers = array('Content-Type: text/html; charset=UTF-8');
$email_content = 'Hello';
wp_mail( $to, $subject, $email_content, $headers );
}else{
$to = 'test@gmail.com';
$subject = 'Email Failed';
$email_content = "Failed";
$email_content .= $gift_to_person_email;
$email_content .= $gift_to_organization_email;
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $email_content, $headers );
}
}
// add the action
add_action( 'woocommerce_new_order', 'custom_email_for_gift',11);
We have tested for woocommerce_new_order and woocommerce_thankyou hook and in both hook it is not working and not sending mail when used thankyou hook and sending failed mail when using woocommerce_new_order and we have checked in database and the email field value is there but also not sending mail