learndash_course_completed

do_action( 'learndash_course_completed',  array $course_data )

Fires after the course is marked completed.


Description #


Parameters #

$course_data

(array) An array of course complete data.


Source #

File: includes/course/ld-course-progress.php


Examples #

 <?php
/**
 * Example usage for learndash_course_completed action.
 */
add_action(
	'learndash_course_completed',
	function( $user, $course_id, $course_progress ) {
		// Add whatever logic to send custom notification to $user->ID
		// based on the value of $course_id or not.
	},
	20,
	3
);

add_action(
	'learndash_course_completed',
	function ( $args = array() ) {
		/*
		$args = array(
		'user' => $current_user,
		'course' => get_post( $course_id ),
		'progress' => $course_progress,
		)
		*/

		if ( isset( $args['user'] ) ) {
			$user_groups = learndash_get_users_group_ids( $args['user']->ID );
			if ( ! empty( $user_groups ) ) {
				foreach ( $user_groups as $group_id ) {
					$all_group_courses_complete = true;

					$group_courses = learndash_group_enrolled_courses( $group_id );
					if ( ! empty( $group_courses ) ) {
						foreach ( $group_courses as $course_id ) {

							$course_status = learndash_course_status( $course_id, $args['user']->ID );
							if ( $course_status != __( 'Completed', 'learndash' ) ) {
								$all_group_courses_complete = false;
								break;
							}
						}
					}

					// If TRUE here then all group courses are completed and the user can be removed.
					if ( $all_group_courses_complete == true ) {

						$group_users = learndash_get_groups_user_ids( $group_id, true );
						if ( in_array( $args['user']->ID, $group_users ) ) {
							$group_users_less = array_diff( $group_users, array( $args['user']->ID ) );
							learndash_set_groups_users( $group_id, $group_users_less );
						}
					}
				}
			}
		}

	}
);

add_action(
	'learndash_course_completed',
	function ( $data ) {

		// Example 1 User ID 1 completes Course 4
		if ( ( $data['user']->ID == 1 ) && ( $data['course']->ID == 4 ) ) {
			// If user 1 has completed course #4 we want to enroll them into course 36

			// LearnDash provides a handy function
			ld_update_course_access( $data['user']->ID, 36 );
		}

		// Example 2 ANY user completed Course 4...
		if ( $data['course']->ID == 4 ) {
			// If user 1 has completed course #4 we want to enroll them into course 36

			// LearnDash provides a handy function
			ld_update_course_access( $data['user']->ID, 36 );
		}

	},
	20
);
 

Changelog #

Changelog
Version Description
2.1.0 Introduced.