在 WooCommerce 中下订单后,根据订单元数据向第三方发送邮件

Send mail to third person based on order meta data once order is placed in WooCommerce

我们使用了 WooCommerce 的结帐字段编辑器,我们在其中创建了额外的电子邮件字段,并且我们尝试在每个新订单后将邮件发送到该电子邮件字段值的电子邮件,这是代码:

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);

我们已经测试了 woocommerce_new_order 和 woocommerce_thankyou 挂钩,在两个挂钩中它都不起作用,并且在使用 thankyou 挂钩时不发送邮件,在使用 woocommerce_new_order 时发送邮件失败,我们已签入数据库并且电子邮件字段值在那里但也没有发送邮件

元数据不需要自定义SQL查询,您可以使用get_meta

所以你得到:

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 = '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', 'action_woocommerce_new_order', 10, 1 );