Skip to content

Instantly share code, notes, and snippets.

@sekanderb
Last active October 4, 2023 04:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sekanderb/cc8a7f942c036ef8f2715242bbdd8e13 to your computer and use it in GitHub Desktop.
Save sekanderb/cc8a7f942c036ef8f2715242bbdd8e13 to your computer and use it in GitHub Desktop.
Enroll students programmatically in Tutor LMS. Documentation link- https://docs.themeum.com/tutor-lms/developers/programmatically-enroll-students/
public function do_enroll($course_id = 0, $order_id = 0, $user_id = 0){
if ( ! $course_id){
return false;
}
do_action('tutor_before_enroll', $course_id);
$user_id = $this->get_user_id($user_id);
$title = __('Course Enrolled', 'tutor')." – ".date_i18n(get_option('date_format')) .' @ '.date_i18n(get_option('time_format') ) ;
$enrolment_status = 'completed';
if ($this->is_course_purchasable($course_id)) {
/**
* We need to verify this enrollment, we will change the status later after payment confirmation
*/
$enrolment_status = 'pending';
}
$enroll_data = apply_filters('tutor_enroll_data',
array(
'post_type' => 'tutor_enrolled',
'post_title' => $title,
'post_status' => $enrolment_status,
'post_author' => $user_id,
'post_parent' => $course_id,
)
);
// Insert the post into the database
$isEnrolled = wp_insert_post( $enroll_data );
if ($isEnrolled) {
do_action('tutor_after_enroll', $course_id, $isEnrolled);
//Mark Current User as Students with user meta data
update_user_meta( $user_id, '_is_tutor_student', time() );
if ($order_id) {
//Mark order for course and user
$product_id = $this->get_course_product_id($course_id);
update_post_meta( $isEnrolled, '_tutor_enrolled_by_order_id', $order_id );
update_post_meta( $isEnrolled, '_tutor_enrolled_by_product_id', $product_id );
update_post_meta( $order_id, '_is_tutor_order_for_course', time() );
update_post_meta( $order_id, '_tutor_order_for_course_id_'.$course_id, $isEnrolled );
}
return true;
}
return false;
}
@rosymehta028
Copy link

Gud

@Juandforero9
Copy link

How to do use it? Please

@sekanderb
Copy link
Author

Hey @juanforero9,

Please read the documentation from here https://docs.themeum.com/tutor-lms/developers/programmatically-enroll-students/

@LeonByer
Copy link

LeonByer commented Nov 18, 2020

Interestingly, the code is writing. In the coming days, I will figure out how to implement it on my computer. In the meantime, I am very busy studying and trying to solve all my problems. To speed up this process, I use the site https://eduzaurus.com/free-essay-samples/the-blind-side/ , which more than once helped me when writing various papers. I want to say that recently on this site among the free samples of an essay found an essay on the film the blind side. Yes, I agree writing the code is a heavier process, but I think I can do it too.

@leandroberg
Copy link

leandroberg commented Mar 27, 2022

Guys, just to help everyone is trying to use this, please, follow the way I've used and worked like a charm. Ps.: has_aplay() is a function we have on our theme, this basically checks if the user has access to the content, see:

# TUTOR - manualy enroll user (enroll data)
add_filter('tutor_enroll_data', function ($data) {
  if ( has_aplay() ) $data['post_status'] = 'completed';
  return $data;
});

# TUTOR - manualy enroll user (do_enroll)
add_action( 'tutor_before_enrolment_check', function ( $course_id, $user_id ) {
  if ( tutor_utils()->is_enrolled($course_id, $user_id) ) {
    return true;
  }

  if ( has_aplay() ) {
    return tutor_utils()->do_enroll($course_id, 0, $user_id);
  }
}, 10, 2 );

Hope this helps. Thanks.

@wandersonce
Copy link

I am trying to enroll all new users automatically on a course that I have, so when we have a new user he will be already enrolled to this course. Does someone know how can I do that?

@AvivoB
Copy link

AvivoB commented Aug 18, 2022

You can use this solution:

This will automatically register any user who registers on the site for the course.

The number 21897 is the course id.

This code is already ready to use, you can use it on your site in the functions.php file of your theme

add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
return tutor_utils()->do_enroll(21897, 0, $user_id);
}

@wandersonce
Copy link

You can use this solution:

This will automatically register any user who registers on the site for the course.

The number 21897 is the course id.

This code is already ready to use, you can use it on your site in the functions.php file of your theme

add_action( 'user_register', 'myplugin_registration_save', 10, 1 ); function myplugin_registration_save( $user_id ) { return tutor_utils()->do_enroll(21897, 0, $user_id); }

Thank you, it worked!

@umardragneel
Copy link

Where to add this code into? Is it functions.php? If so, how to enroll to multiple students?

for example, I want to enroll course_id #25 to user_id #11, #12, and #13 with order_id #2511, #2512, and #2513 each.

@AvivoB
Copy link

AvivoB commented Dec 25, 2022

25
Hello, first check if these users are already registered on your site.

If they are, it is better to do it manually from the back-office via the Tutor plugin.

If you want to make sure that each user is registered to this course when he/she registers on the site, write the code I put above and specify the course id.

@umardragneel
Copy link

25
Hello, first check if these users are already registered on your site.

If they are, it is better to do it manually from the back-office via the Tutor plugin.

If you want to make sure that each user is registered to this course when he/she registers on the site, write the code I put above and specify the course id.

These users are already registered on my site. I want to enroll hundreds of students (registered users) to one specific course, but I don't know how to write the code, and where to put it.

Is it the functions.php or where?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment