Last active
October 4, 2023 04:07
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
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
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.