php – Learndash Notifications – Send notification if course is not completed yet
I have users who are required to complete a course in 15 days. I am trying to setup reminder notifications for users who have not yet completed the course yet. I don’t want the course to expire in 15 days because if a user has not yet completed the course, I want them to still be able to login and complete it. For this reason I am setting the course to expire at 100 days and sending reminder notifications 90 days before that using the “”X” Days before the course expires” trigger. The problem is that I only want to send these emails if they have not yet completed the course. Below is the relevant code from the Learndash Notification plugin.
/**
* Send learndash notification email when user's course is about to expire in X days
*/
function learndash_notifications_course_expires() {
// Fired in cron.php
// Get all courses
$courses = learndash_notifications_get_all_courses();
// Get all notifications
$notifications = learndash_notifications_get_notifications( 'course_expires' );
// Foreach courses
foreach ( $courses as $c ) {
$c_meta = get_post_meta( $c->ID, '_sfwd-courses', true );
$c_meta = maybe_unserialize( $c_meta );
// If course doesn't has expiration setting, continue
if ( ( ! isset( $c_meta['sfwd-courses_expire_access'] ) || ( isset( $c_meta['sfwd-courses_expire_access'] ) && $c_meta['sfwd-courses_expire_access'] != 'on' ) )
||
( ! isset( $c_meta['sfwd-courses_expire_access_days'] ) || ( isset( $c_meta['sfwd-courses_expire_access_days'] ) && $c_meta['sfwd-courses_expire_access_days'] == 0 ) ) ) {
continue;
}
// Course access list
$c_access_list = learndash_get_users_for_course( $c->ID );
$c_access_list = ! empty( $c_access_list ) && is_a( $c_access_list, 'WP_User_Query' ) ? $c_access_list->get_results() : [];
// If course has no access list, continue
if ( empty( $c_access_list ) ) continue;
$c_access_days = (int) $c_meta['sfwd-courses_expire_access_days'];
// Foreach users who have access
foreach ( $c_access_list as $u_id ) {
$allow = apply_filters( 'learndash_notifications_send_course_expires_notification_for_completed_users', true, $u_id, $c );
if ( ! $allow && learndash_course_completed( $u_id, $c->ID ) ) {
continue;
}
$access_from = (int) get_user_meta( $u_id, 'course_' . $c->ID . '_access_from', true );
// Foreach notifications
foreach ( $notifications as $n ) {
$n_days = get_post_meta( $n->ID, '_ld_notifications_course_expires_days', true );
// If users' course access is equal to setting, send notifications
if ( ! empty( $access_from ) && date( 'Y-m-d H' ) == date( 'Y-m-d H', strtotime( '-' . $n_days . ' days', strtotime( '+' . $c_access_days . ' days', $access_from ) ) ) ) {
learndash_notifications_send_notification( $n, $u_id, $c->ID );
}
}
}
}
}
If I change the line
foreach ( $c_access_list as $u_id ) {
$allow = apply_filters( 'learndash_notifications_send_course_expires_notification_for_completed_users', true, $u_id, $c );
from true to false, will that mean that the emails are not sent if the user has completed the course?
Thanks!
Leave an answer