1

My LearnDash plugin is configured to have shared course steps, so that I can use the same lessons across multiple courses. However, if a users marks a lesson as complete or incomplete in one particular course, it is not reflected across all courses (default setting). I want to make it such that any change in the completion status of a shared lesson is reflected across all courses containing that lesson.

This is the code that I used to achieve the desired result, but it always results in a critical error.

function sync_lesson_completion($lesson_id, $user_id, $course_id) {
    // Get the course that contains the lesson
    $course = get_post($course_id);
    
    // Check if the course has shared steps enabled
    $shared_steps_enabled = get_post_meta($course_id, '_sfwd-courses_sharedsteps_enabled', true);
    
    // Check if shared steps are enabled and the lesson is shared
    if ($shared_steps_enabled && !empty($lesson_id)) {
        // Get all the courses where the lesson is shared
        $shared_courses = learndash_get_courses_by_step($lesson_id, 'sfwd-lessons');
        
        // Loop through the shared courses
        foreach ($shared_courses as $shared_course_id) {
            // Skip the current course
            if ($shared_course_id == $course_id) {
                continue;
            }
            
            // Mark the lesson as completed for the user in the shared course
            learndash_update_user_activity($user_id, $lesson_id, 'sfwd-lessons', $shared_course_id, true);
        }
    }
}

// Hook into the lesson completion action
add_action('learndash_lesson_completed', 'sync_lesson_completion', 10, 3);

What am I doing wrong and how do I achieve the desired result?

0 Answers0