-
-
Save sekanderb/cc8a7f942c036ef8f2715242bbdd8e13 to your computer and use it in GitHub Desktop.
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; | |
} |
How to do use it? Please
Hey @juanforero9,
Please read the documentation from here https://docs.themeum.com/tutor-lms/developers/programmatically-enroll-students/
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.
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.
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?
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);
}
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!
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.
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.
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?
Gud