Filter uncanny-automator-pro

automator_tutorlms_course_force_cancel

Filters the course cancellation process in Tutor LMS, allowing customization when a course is forcefully canceled.

add_filter( 'automator_tutorlms_course_force_cancel', $callback, 10, 1 );

Description

Filters the force cancel status for Tutor LMS course enrollments. Allows developers to override the default behavior and force a user's course cancellation, even if standard unenrollment methods fail. Useful for handling complex scenarios like WooCommerce status changes.


Usage

add_filter( 'automator_tutorlms_course_force_cancel', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

<?php

/**
 * Example of how to force cancel a user's Tutor LMS course enrollment.
 *
 * This filter is designed to override the default behavior and force
 * the cancellation of a user's enrollment in a Tutor LMS course,
 * potentially bypassing certain checks or conditions.
 *
 * @param bool $force_cancel Default is false. Set to true to force cancellation.
 * @return bool True if the enrollment should be forcefully canceled, false otherwise.
 */
function my_automator_tutorlms_force_cancel_enrollment( $force_cancel ) {
    // In a real-world scenario, you might check for specific conditions
    // or user roles before deciding to force the cancellation.
    // For this example, we'll simply force it if a specific query parameter is set.

    if ( isset( $_GET['force_tutorlms_cancel'] ) && '1' === $_GET['force_tutorlms_cancel'] ) {
        return true;
    }

    // If no specific condition is met, return the original value (false by default).
    return $force_cancel;
}

// Hook the filter function to the 'automator_tutorlms_course_force_cancel' hook.
// The priority is set to 10 (default) and it accepts 1 argument ($force_cancel).
add_filter( 'automator_tutorlms_course_force_cancel', 'my_automator_tutorlms_force_cancel_enrollment', 10, 1 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

uncanny-automator-pro/src/integrations/tutorlms/actions/tutorlms-courseunenroll.php:149

public function cancel_user_enrollment( $user_id, $course_id ) {

		$force_update = apply_filters( 'automator_tutorlms_course_force_cancel', false );

		if ( true === $force_update ) {

			global $wpdb;

			// Force update, because their utility function does not work for WooCommerce status change from 3rd-party API gateway.
			// This code is taken from their utility as well.
			$wpdb->update(
				$wpdb->posts,
				array( 'post_status' => 'canceled' ),
				array(
					'post_type'   => 'tutor_enrolled',
					'post_author' => $user_id,
					'post_parent' => $course_id,
				)
			);

			return true;

		}

		// This utility does not return anything useful.
		tutor_utils()->cancel_course_enrol( $course_id, $user_id );

		return true;

	}

Scroll to Top