Foreach循环中的wc_mail仅发送第一封电子邮件

时间:2019-06-06 作者:Renan A. Diaz

我正在尝试从db查询在foreach循环中发送电子邮件,这里一切正常,但我需要为表中的每个id发送电子邮件,并且只工作一次,但没有显示任何错误,似乎一切正常!。代码如下:

        public function praga_emails(){

            global $wpdb;
            // $results = $wpdb->get_results( "SELECT * FROM `wpbu_postmeta` WHERE `meta_key` = \'slzexploore_tbook_order\' AND `meta_value` = \'". $order_id ."\' ORDER BY wpbu_postmeta.post_id", ARRAY_A ); 

            $results = $wpdb->get_results( "SELECT * FROM `wpbu_praga_user_notice`", ARRAY_A ); 
            $errors = array();

            if ( $wpdb->num_rows !== 0 ) {

                foreach ($results as $result) { 

                    $send = $this->praga_emails_after_order( $result[\'tbook\'] ); 

                    if ( !$send ) {
                        $errors[] = $result[\'tbook\'];
                    }

                }

            }  

            echo json_encode( [ \'code\' => 1, \'errors\' => $errors ] );
            wp_die();

    } 

    public function praga_emails_after_order( $reservacion_id = null ){

        global $wpdb;

        if ( $reservacion_id === null ) {
            return false;
        }

                    $reservacion = get_post( $reservacion_id, ARRAY_A );
                    $reservacion_meta = get_post_meta( $reservacion_id );

                    $tour_id = get_post_meta( $reservacion_id, \'slzexploore_tbook_tour\', true );
                    $tour = get_post( $tour_id, ARRAY_A ); 

                    // Get an instance of the WC_Order object
                    $order = new WC_Order( $reservacion_meta[\'slzexploore_tbook_order\'][0] );
                    $order_id = $order->get_id();

                    // -------- Confirm -----------
                    ob_start();
                        include_once( CUSTOM_PRAGA_DIR . \'/templates/email-confirm.php\');
                    $content = ob_get_contents(); 
                    ob_end_clean();
                    ob_end_flush(); 

                    //format the email
                    $recipient = $order->get_billing_email(); 
                    $subject = esc_html__(\'Tu Reserva ha sido confirmada:\', \'custom-praga\') . \' \' . get_post_meta( $tour_id, \'slzexploore_tour_display_title\', true); 

                    $headers = "Content-Type: text/html\\r\\n";
                    //send the email through wordpress
                    wc_mail( $recipient, $subject, $content, $headers ); 


                    // -------- Admin -----------

                    ob_start();
                        include_once( CUSTOM_PRAGA_DIR . \'/templates/email-admin.php\');
                    $admin_content = ob_get_contents(); 
                    ob_end_clean();
                    ob_end_flush(); 

                    //format the email
                    $admin_recipient = Redux::getOption(\'custom_praga\', \'email-admin\'); 
                    $admin_subject = esc_html__(\'Nueva Reserva:\', \'custom-praga\') . \' \' . get_post_meta( $tour_id, \'slzexploore_tour_display_title\', true); 

                    $admin_headers = "Content-Type: text/html\\r\\n";
                    //send the email through wordpress
                    wc_mail( $admin_recipient, $admin_subject, $admin_content, $admin_headers );

                    // -------- Proveedor -----------

                    // DATE
                    $sqldate = get_post_meta( $reservacion_id, "slzexploore_tbook_tour_date", true );
                    // BEGIN EMAIL
                    ob_start();
                      $euro = get_post_meta( $reservacion_id, \'slzexploore_tbook_deposit_amount\', true );
                      $tasa = Redux::getOption(\'custom_praga\', \'exchange-rate\');
                      $price = $euro * $tasa; 
                      include_once( CUSTOM_PRAGA_DIR . \'/templates/email-proveedor.php\');
                    $proveedor_content = ob_get_contents(); 
                    ob_end_clean();
                    ob_end_flush();

                    //format the email 
                    $terms_p = get_the_terms( $tour_id, \'proveedor\' ); 
                    $proveedor_recipient = get_term_meta( $terms_p[0]->term_id, \'correo\', true ); 
                    // $proveedor_recipient = \'[email protected]\';
                    $proveedor_subject = esc_html__(\'Reserva\', \'custom-praga\'); 

                    $proveedor_headers = "Content-Type: text/html\\r\\n";
                    //send the email through wordpress
                    wc_mail( $proveedor_recipient, $proveedor_subject, $proveedor_content, $proveedor_headers );

                    //Prevent multiple emails
                    $wpdb->delete( \'wpbu_praga_user_notice\', array( \'tbook\' => $reservacion_id ) );

                    return true;


    }
praga_emails 是一个ajax函数,并且praga_emails_after_order 发送3封带有3个不同模板的电子邮件。这只起作用一次,有什么想法吗?

谢谢

1 个回复
SO网友:nmr

您使用include_once 在里面praga_emails_after_order() 因此,文件(模板:email confirm.phpemail admin.phpemail proveedor.php)将仅包含在第一次迭代中。

尝试使用include 而不是include_once, 下一封电子邮件可能因为内容空而无法发送。

相关推荐

Cron not sending wp-mail()

我对cron计划功能有问题,它不想用wp_mail() 作用This is my code:它被放置在函数中。php<?php add_filter(\'cron_schedules\', \'add_review_invites_schedule\'); function add_review_invites_schedule($schedules){ $schedules[\'send_review_invites_interva