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