Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actions not firing when they should #1479

Open
idressos opened this issue Dec 13, 2024 · 2 comments
Open

Actions not firing when they should #1479

idressos opened this issue Dec 13, 2024 · 2 comments

Comments

@idressos
Copy link

idressos commented Dec 13, 2024

In our intended use, it is important that if a course enrollment expires, and the user re-enrolls, they have to start over. Thus, I'm trying to make a custom plugin that automatically deletes any cancelled enrollment, but the following Tutor LMS actions never seem to fire:

  • tutor_after_enrollment_cancelled
  • tutor/course/enrol_status_change/after

I have added debug code to every part of my custom code and I'm certain that the actions are not firing as they should. The plugin is being initialized correctly.

Moreover, in another custom plugin I'm making, I noticed that the tutor_after_enroll action is also not fired if the user PURCHASES the course instead of enrolling for free (in case the course is paid).

I have spent a lot of time trying to describe this to a support agent in a ticket (we have a Tutor LMS Pro subscription) but they either don't understand what I'm saying, they are not even reading what I'm sending, or the replies are AI-generated :/

My code (from the first section of my issue):

/**
 * Plugin Name: Tutor Canceled Enrollment Cleaner
 * Description: Permanently deletes Tutor enrollments when they are canceled.
 * Version: 1.0.0
 * Author: Ioannis Dressos
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

class TutorCanceledEnrollmentCleaner {

    public static function init() {
        add_action( 'tutor_after_enrollment_cancelled', [ __CLASS__, 'auto_delete_enrollment' ], 10, 2 );
        add_action( 'tutor/course/enrol_status_change/after', [ __CLASS__, 'auto_delete_enrollment_by_id' ], 10, 2 );
    }
    
    /**
     * Automatically deletes the enrollment when it is canceled.
     *
     * @param int $course_id The ID of the course.
     * @param int $user_id The ID of the user.
     */
    public static function auto_delete_enrollment( $course_id, $user_id ) {
        tutor_utils()->cancel_course_enrol( $course_id, $user_id, 'delete' );
    }

    public static function auto_delete_enrollment_by_id ( $enrol_id, $new_status ) {
        $enrollment = tutor_utils()->get_enrolment_by_id( $enrol_id );
        
        $course_id = $enrollment->course_id;
        $user_id = $enrollment->student_id;

        if ( $new_status === 'cancel' || $new_status === 'canceled' ) {
            self::auto_delete_enrollment( $course_id, $user_id );
        }
    }
}

// Initialize the plugin.
TutorCanceledEnrollmentCleaner::init();```
@idressos
Copy link
Author

@Matthias-Ab

@idressos
Copy link
Author

Another example of clean, simple code that is not working even though it should be:

<?php
/**
 * Plugin Name: Tutor Re-Enroll Progress Reset
 * Description: Resets user course progress when re-enrolling.
 * Version: 1.0.0
 * Author: Ioannis Dressos
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

class TutorReenrollProgressReset {

    public static function init() {
        add_action( 'tutor_after_enroll', [ __CLASS__, 'reset_course_progress' ], 10, 2 );
        add_action( 'tutor/course/enrol_status_change/after', [ __CLASS__, 'reset_course_progress_by_enrollment_id' ], 10, 2 );
    }
    
    public static function reset_course_progress( $course_id, $user_id ) {
        tutor_utils()->delete_course_progress( $course_id, $user_id );
    }
    
    public static function reset_course_progress_by_enrollment_id ( $enrol_id, $new_status ) {
        $enrollment = tutor_utils()->get_enrolment_by_id( $enrol_id );
        
        $course_id = $enrollment->course_id;
        $user_id = $enrollment->student_id;

        if ( $new_status === 'completed' ) {
            self::reset_course_progress( $course_id, $user_id );
        }
    }
}

// Initialize the plugin.
TutorReenrollProgressReset::init();

We need the actions fired appropriately regardless if enrollment/cancellation/deletion was done through the admin panel, in batch, from clicking the button (free courses), or by purchase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant