I'm trying to send emails in a foreach loop from a db query and all fine here, but i need to send emails fo each id on the table and only works once, but is not showing any error, seems all fine!. Here is the code:
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
is an ajax function, and praga_emails_after_order
sends 3 emails with 3 different templates. This is working only once, any thoughts ?
Thanks!