John Davidson

php - WooCommerce send email on a scheduled date

0 comments
Message:


I am working on a custom Wordpress / WooCommerce plugin development, the point is: customer orders a medical treatment and schedules treatment date (for an instance, he orders today and schedules his treatment in 16 days 2/28/2021), plugin should check that date in the db table (let's assume that we fetch that from wp_usermeta table under meta_key => 'treatment_date') and set a trigger mail function 7 days before the treatment date. Mail function should e-mail customer that he's left 7 days until his treatment.


This is going to be developed as a custom plugin.


Any ideas how should look like that mail trigger function?



My Answer:
treatment_date) and send an email reminder to the customer on that scheduled date.

Here is a sample code snippet to achieve this functionality:

php
// Hook into the scheduled event
add_action('send_reminder_email_event', 'send_reminder_email');

function send_reminder_email() {
// Get all users who have scheduled treatment date today
$users = get_users(array(
'meta_key' => 'treatment_date',
'meta_value' => date('Y-m-d'),
'meta_compare' => '='
));

foreach ($users as $user) {
$to = $user->user_email;
$subject = 'Reminder: Your scheduled treatment date is today';
$message = 'Dear customer, this is a reminder that your scheduled treatment date is today. Please make sure to attend your appointment.';

wp_mail($to, $subject, $message);
}
}

// Schedule the event to run daily
function schedule_reminder_email_event() {
if (!wp_next_scheduled('send_reminder_email_event')) {
wp_schedule_event(time(), 'daily', 'send_reminder_email_event');
}
}
add_action('wp', 'schedule_reminder_email_event');


In this code snippet, we first define a function `send_reminder_email` that fetches all users from the database who have a treatment date scheduled for today and sends them a reminder email. We then hook this function to a scheduled event `send_reminder_email_event`.

We also define another function `schedule_reminder_email_event` that schedules the event to run daily. This ensures that the reminder emails are sent out every day to users who have a treatment scheduled for that day.

You can add this code to your custom plugin file and modify it as needed to fit your specific requirements.

Rate this post

4 of 5 based on 7153 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us