Im having a bit of issue with learndash course autocomplete for some reason lessons are not being marked as completed anymore after all topics are finished. Im trying to achieve this function using bellow code
add_action('learndash_topic_completed', 'custom_check_lesson_completion');
function custom_check_lesson_completion() {
if (!empty($_POST)) {
error_log('POST Data: ' . print_r($_POST, true));
}
// Check if $_POST variable contains topic_id
if (isset($_POST['post'])) {
// Retrieve the topic ID
$topic_id = intval($_POST['post']);
// Get lesson ID associated with the completed topic
$lesson_id = learndash_get_setting($topic_id, 'lesson');
// Check if the lesson is set
if (!empty($lesson_id)) {
// Get all topics associated with the lesson
$lesson_topics = learndash_get_topic_list($lesson_id);
// Check if all topics in the lesson are completed
$all_completed = true;
foreach ($lesson_topics as $lesson_topic) {
if (!learndash_is_topic_complete(get_current_user_id(), $lesson_topic)) {
$all_completed = false;
break;
}
}
// If all topics are completed, mark the lesson as completed
if ($all_completed) {
$this_lesson = get_post($lesson_id);
// learndash_update_completion(get_current_user_id(), $lesson_id);
learndash_mark_complete_process($this_lesson);
}
}
}
}
i've tried multiple variations of the above code implementing different functions from this site https://developers.learndash.com/
But none of them seems to set the course progression to complete i've also tried just circumventing this by using the bellow code which just checks course progression and changes it if it is above a certain percent
add_action( 'learndash_user_course_progress_updated', 'custom_mark_course_completed', 10, 5 );
function custom_mark_course_completed( $user_id, $course_id, $course_progress, $course_info, $ld_course ) {
// Check if course progress is above 96%
if ( $course_progress > 96 ) {
// Set course progress to 100%
learndash_update_user_course_progress( $user_id, $course_id, 100 );
// Mark course as completed
learndash_mark_course_completed( $user_id, $course_id );
}
}```
i believe my fault is possibly outdated learndash functions however i cant seem to find any info on that i would appreciate any help thanks
Issue confirmed
I had exactly the same issue: completing all LearnDash topics sometimes doesn't result in the associated lesson being marked as complete.
I say 'sometimes' as for a clean test course, with a test user whose LearnDash data had been cleared:
Solution (it works for me!)
Using your (great!) code as a base with just a couple of tweaks, I believe I've got it working in that two complete test runs consistently show the lessons being marked as complete automatically when its topics are complete.
Two tweaks
Tweak 1 - your code to check if all topics are complete:
3rd line, I changed
$lesson_topicto$lesson_topic->IDas$lesson_topicis a WP_Post object, but the function requires an Int post ID, otherwise I encountered errors.Tweak 2 - your code to mark the associated lesson as complete if all it's topics are complete:
4th line, I had to change
learndash_mark_complete_process($this_lesson);tolearndash_mark_complete_process(get_current_user_id(), $this_lesson, true);As I say, I've only run this through two tests but it seems to do what's required. Many thanks for your code, by the way, it saved me quite a lot of work. Hoping this works for you!