Ad

Send Mail To Third Person Based On Order Meta Data Once Order Is Placed In WooCommerce

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 = '[email protected]';
        $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

Ad

Answer

Custom SQL queries are not needed for the meta data, you can use get_meta

So you get:

function action_woocommerce_new_order( $order_id ) {
    // Getting an instance of WC_Order object
    $order = wc_get_order( $order_id );
    
    // Is a WC_Order
    if ( is_a( $order, 'WC_Order' ) ) { 
        // Get meta
        $gift_to_person_email = $order->get_meta( 'gift_to_person_email' );
        $gift_to_organization_email = $order->get_meta( 'gift_to_organization_email' );
        
        // Initialize
        $to = '';

        // NOT empty
        if( ! empty( $gift_to_person_email ) ) {
            $to = $gift_to_person_email;
        } elseif( ! empty( $gift_to_organization_email ) ) {
            $to = $gift_to_organization_email;
        }
        
        // Get billing first name
        $name = $order->get_billing_first_name();
        
        // NOT empty
        if( ! empty ( $to ) ) {
            $to = $to;
            $subject = 'The subject';
            $email_content = 'hello';
            $headers = array( 'Content-Type: text/html; charset=UTF-8' );
            
            wp_mail( $to, $subject, $email_content, $headers );
        } else {
            $to = '[email protected]';
            $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', 'action_woocommerce_new_order', 10, 1 );
Ad
source: stackoverflow.com
Ad