ulgm_subscription_license_drop_users_courses
Filters whether to drop users from courses when a subscription is revoked.
add_filter( 'ulgm_subscription_license_drop_users_courses', $callback, 10, 1 );
Description
This filter allows developers to control whether a user's access to courses associated with a subscription should be dropped when the subscription status changes. By default, access is retained. Developers can return `false` to immediately revoke course access.
Usage
add_filter( 'ulgm_subscription_license_drop_users_courses', 'your_function_name', 10, 1 );
Parameters
-
$subscription(mixed) - This parameter determines whether to switch the group to draft status.
Return Value
The filtered value.
Examples
add_filter( 'ulgm_subscription_license_drop_users_courses', 'my_custom_ulgm_drop_users_courses', 10, 2 );
/**
* Customize which users and courses are dropped when a WooCommerce subscription license expires or is cancelled.
*
* This filter allows developers to conditionally prevent the dropping of users or courses
* from a group based on specific criteria. For example, you might want to retain certain
* users or courses if the subscription is temporarily put on hold rather than fully cancelled.
*
* @param bool $should_drop_courses_users Whether to drop users and courses from the group. Default is true.
* @param WC_Subscription $subscription The current WooCommerce Subscription object.
* @return bool The modified value indicating whether to drop users and courses.
*/
function my_custom_ulgm_drop_users_courses( $should_drop_courses_users, $subscription ) {
// Example: If the subscription is simply on hold, we might not want to drop users/courses yet.
// We assume the source context indicates a pending cancellation or other status that warrants dropping.
// If you wanted to *prevent* dropping in specific scenarios, you would return false here.
// For demonstration, we'll just return the original value unless a specific condition is met.
// Let's assume there's a meta key on the subscription that indicates a special type.
// If this meta key exists and is set to 'lifetime', we might want to keep access.
$subscription_type = $subscription->get_meta( '_subscription_type', true );
if ( 'lifetime' === $subscription_type ) {
// Do not drop users or courses if it's a lifetime subscription.
return false;
}
// Otherwise, proceed with the default behavior of dropping users and courses.
return $should_drop_courses_users;
}
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
src/classes/woocommerce/woocommerce-license-subscription.php:669
src/classes/woocommerce/woocommerce-license-subscription.php:774
public function change_group_to_draft( WC_Subscription $subscription ) {
if ( true !== apply_filters( 'ulgm_subscription_status_switch_group_to_draft', true, current_action(), $subscription, $this ) ) {
return;
}
/**
* Let the user have access to group until the subscription runs out OR admin cancels it
*/
if ( 'woocommerce_subscription_status_pending-cancel' === current_action() ) {
return;
}
$order_id = $subscription->get_last_order( 'ids', array( 'parent' ) );
$order = wc_get_order( $order_id );
// Is valid Order?
if ( ! $order instanceof WC_Order ) {
return;
}
$group_id = $order->get_meta( SharedFunctions::$linked_group_id_meta, true );
if ( empty( $group_id ) || ! is_numeric( $group_id ) ) {
return;
}
if ( false === apply_filters( 'ulgm_subscription_license_drop_users_courses', true, $subscription ) ) {
return;
}
/**
* Remove users access from Group
*/
$users = LearndashFunctionOverrides::learndash_get_groups_user_ids( $group_id );
if ( $users ) {
foreach ( $users as $user_id ) {
ld_update_group_access( $user_id, $group_id, true );
$transient_key = "learndash_user_groups_{$user_id}";
delete_transient( $transient_key );
}
}
/**
* Remove courses from Group
*/
$group_course_ids = LearndashFunctionOverrides::learndash_group_enrolled_courses( $group_id );
if ( $group_course_ids ) {
foreach ( $group_course_ids as $course_id ) {
ld_update_course_group_access( $course_id, $group_id, true );
$transient_key = "learndash_course_groups_{$course_id}";
delete_transient( $transient_key );
}
}
update_post_meta( $group_id, 'uo_group_courses', $group_course_ids );
update_post_meta( $group_id, 'uo_group_users', $users );
$post = get_post( $group_id );
if ( $post instanceof WP_Post ) {
$post->post_status = 'draft';
wp_update_post( $post );
}
switch ( current_action() ) {
case 'woocommerce_subscription_status_expired':
$new_status = 'expired';
break;
case 'woocommerce_subscription_status_on-hold':
$new_status = 'on-hold';
break;
case 'woocommerce_subscription_status_pending-cancel':
$new_status = 'pending-cancel';
break;
case 'woocommerce_subscription_status_cancelled':
default:
$new_status = 'cancelled';
break;
}
$this->change_subscription_statuses( $group_id, $subscription, $new_status );
}