• Resolved ramon135

    (@ramon135)


    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);
    • This topic was modified 2 years, 9 months ago by ramon135.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello,

    Since this is related to the Learndash plugin, I’d suggest checking with the plugin developer to see if there is a way to validate the user enrollment. You can request support for the Learndash plugin here: https://www.learndash.com/support/

    We’ve not heard back from you in a while, so I’m marking this thread as resolved. Hopefully, you were able to find a solution to your problem! If you have other questions, please feel free to open a new topic.

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.