Browse: Home / Snippets /

Remove user from group after completing all group courses

Contents


Snippet #

Important: All snippets are provided as-is without support or guarantees. These snippets are provided as guidelines for advanced users looking to customize LearnDash. For any additional help or support with these snippets, we recommend reaching out to a LearnDash Expert.

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 );
					}
				}
			}
		}
	}
	
});