Home › Forums › General Issues › Auto send REVIEW REQUEST email via Cron Job
I have created a code to auto send booking emails after a booking status is updated. Depending on the booking status, a certain e-mail text is called. This function works perfectly so feel free to use it 😉
// Send notification emails to guests
function yl_send_booking_email_after_status_update($post_id) {
// Get submitted values.
$values = $_POST['acf'];
// Check if a specific value was updated.
if ( isset($_POST['acf']['field_5fbcef66c1d3f']) ) {
$send_email = $_POST['acf']['field_5fbcef66c1dce'];
$booking_status = $_POST['acf']['field_5fbcef66c1d3f'];
$to = $_POST['acf']['field_5fbcef66c18b8'];
if ( $send_email == 'yes' && $booking_status != 'gesloten' ) {
if ( $booking_status == 'bevestigd' ) {
$subject = get_field('booking_email_subject_confirmed', 'booking_settings');
$message = get_field('booking_email_message_confirmed', 'booking_settings');
} elseif ( $booking_status == 'gewijzigd' ) {
$subject = get_field('booking_email_subject_changed', 'booking_settings');
$message = get_field('booking_email_message_changed', 'booking_settings');
} elseif ( $booking_status == 'geannuleerd' ) {
$subject = get_field('booking_email_subject_canceled_by_guest', 'booking_settings');
$message = get_field('booking_email_message_canceled_by_guest', 'booking_settings');
} elseif ( $booking_status == 'door ons geannuleerd' ) {
$subject = get_field('booking_email_subject_canceled_by_us', 'booking_settings');
$message = get_field('booking_email_message_canceled_by_us', 'booking_settings');
}
$headers = array
(
'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>',
'X-Mailer: PHP/' . phpversion(),
'MIME-Version: 1.0',
'Content-type: text/html; charset=iso-8859-1'
);
$headers = implode( "\r\n" , $headers );
wp_mail( $to, $subject, $message, $headers );
}
}
}
add_action('acf/save_post', 'yl_send_booking_email_after_status_update', 5);
Now I want to chanllange myself a bit further. I want to auto send (via cron job) a ‘Review request’ email ‘X hours’ after the booking date/time. But I’m stuck for a while now.
Emails do get send but only with pre text. The actuall content of the booking (name, date, time, etc) is left blank.
When I use $post_id in the function, no emails are being send. I hope I made myself a bit clear.
Here’s what I have:
// Send review emails to guests
function yl_send_review_email_function($post_id) {
// Get submitted values.
$values = $_POST['acf'];
$send_email = $_POST['acf']['field_5fbcef66c1dce'];
$booking_status = $_POST['acf']['field_5fbcef66c1d3f'];
$review_delay = get_field('booking_settings_review_delay', 'booking_settings');
$to = '[email protected]';
$subject = get_field('booking_email_subject_review', 'booking_settings');
$message = get_field('booking_email_message_review', 'booking_settings') . '<br /><br />' . $send_email . '<br /><br />' . $booking_status . ' dit no ' . $review_delay;
$headers = array
(
'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>',
'X-Mailer: PHP/' . phpversion(),
'MIME-Version: 1.0',
'Content-type: text/html; charset=iso-8859-1'
);
$headers = implode( "\r\n" , $headers );
wp_mail( $to, $subject, $message, $headers );
$status = 'gesloten';
update_field( 'booking_status', $status );
}
add_action('acf/save_post', 'yl_send_review_email_function', 5);
// Time schedule for Cron Job Event
function yl_cron_schedules($schedules){
if(!isset($schedules["1min"])){
$schedules["1min"] = array(
'interval' => 1*60,
'display' => __('Once every minute')
);
}
if(!isset($schedules["5min"])){
$schedules["5min"] = array(
'interval' => 5*60,
'display' => __('Once every 5 minutes')
);
}
if(!isset($schedules["30min"])){
$schedules["30min"] = array(
'interval' => 30*60,
'display' => __('Once every 30 minutes')
);
}
return $schedules;
}
// Schedule Cron Job Event
if (!wp_next_scheduled('yl_booking_review_mailer')) {
//You can now use '5min', '20min' or any of the default here
wp_schedule_event( time(), '1min', 'yl_booking_review_mailer' );
}
add_action( 'yl_booking_review_mailer', 'yl_send_review_email_function' );
PS. I left the needed IF STATEMENTS out to be sure that’s nog stopping emails going out.
your problem is that the post is not being submitted and that means that $_POST[‘acf’] will be empty.
You need to get the values using get_field() instead of looking in $_POST[‘acf’].
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.