How to validate if the user is already enrolled into the course?
-
I am creating an LMS using Learndash + Woocommerce plugin. I am struggle to achieve on validate that the user is already enrolled to the course. For example, the user want to buy the Course A so when the user click the Add to Cart button, a message will said “You’ve already enrolled into Course A”. Then the user will select Course C then it will add to their cart. When I run my code to the website, it didn’t allow to add to the cart. It will show me a message “You’ve already enrolled to the course”.
function validate_user_enrolled($courses, $current_user) { $current_user = wp_get_current_user(); $courses = learndash_user_get_enrolled_courses($current_user -> ID, array(), false); /* $lessons = learndash_get_course_lessons_list( $courses[0], $current_user -> ID, array(), false); */ //Check if this user is already enrolled on this course. if(sfwd_lms_has_access($courses, $current_user -> ID) ) { wc_add_notice( __( 'You have already enrolled. ' , 'woocommerce' ), 'error' ); return false; // course already taken } return true; // course not taken } add_action('woocommerce_add_to_cart_validation', 'validate_user_enrolled', 10, 2);
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘How to validate if the user is already enrolled into the course?’ is closed to new replies.