tutor
tutor copied to clipboard
Avoid students to be enrolled automatically after the payment (EDD)
In order to make this possible is needed to avoid students to be enrolled in the first place, which means after they buy a course. The filter hook tutor_enroll_data would be the first choice but...that's not possible with EDD (no idea about the other payment options) because the function edd_update_payment_status calls complete_course_enroll at the end (so the course starts immediately after the payment).
This is what I had to do since no hook was present in the EDD code to stop the enrolment.
add_action('edd_update_payment_status', function( $payment_id, $new_status, $old_status ){
global $wpdb;
// when a new payment is completed
if( $old_status=='pending' && $new_status=='publish' ) {
$enrolled_ids_with_course = tutor_utils()->get_course_enrolled_ids_by_order_id($payment_id);
if ($enrolled_ids_with_course){
$enrolled_ids = wp_list_pluck($enrolled_ids_with_course, 'enrolled_id');
if (is_array($enrolled_ids) && count($enrolled_ids)){
foreach ($enrolled_ids as $enrolled_id){
// tutor_utils()->complete_course_enroll($payment_id);
// this last call of "edd_update_payment_status" makes post_status=='complete': we want to revert that to pending so...
// ...we immediately switch back the enrollment status to 'pending'
$wpdb->update( $wpdb->posts, array( 'post_status' => 'pending' ), array( 'ID' => $enrolled_id ) );
}
}
}
}
}, 99, 3);
// if priority>10 fires after:
// add_action('edd_update_payment_status', array($this, 'edd_update_payment_status'), 10, 3);
// https://github.com/themeum/tutor/blob/54984cd5e81e35dc9ab0b5809e2c218ef9cfc4e9/classes/TutorEDD.php
Hope this can help others or to improve LMS Tutor 🙏