ulgm_subscription_status_switch_group_to_draft
Filters the subscription status when switching a group to draft, allowing modification of the resulting status.
add_filter( 'ulgm_subscription_status_switch_group_to_draft', $callback, 10, 2 );
Description
Filters whether a subscription group should be automatically switched to draft status. Developers can use this to prevent or modify the automatic transition, for example, to keep access active longer. The hook fires when a subscription's status changes, before the draft transition is applied.
Usage
add_filter( 'ulgm_subscription_status_switch_group_to_draft', 'your_function_name', 10, 2 );
Parameters
-
$subscription(mixed) - This parameter contains the name of the current action being performed by WordPress.
-
$this(mixed) - This parameter contains the `WC_Subscription` object that is currently being processed.
Return Value
The filtered value.
Examples
/**
* Example of how to use the ulgm_subscription_status_switch_group_to_draft filter.
*
* This example demonstrates how to prevent a group from being set to 'draft'
* when a subscription is in a 'pending-cancel' status, allowing the user to
* retain access until the subscription truly expires.
*/
add_filter(
'ulgm_subscription_status_switch_group_to_draft',
function ( $should_switch, $current_action, WC_Subscription $subscription, $object ) {
// If the current action is 'woocommerce_subscription_status_pending-cancel',
// we want to prevent the group from being switched to draft immediately.
// This allows the user to continue having access until the actual expiry.
if ( 'woocommerce_subscription_status_pending-cancel' === $current_action ) {
// Return false to indicate that the group should NOT be switched to draft.
return false;
}
// For all other actions, allow the default behavior to proceed.
return $should_switch;
},
10, // Priority
4 // Accepted arguments: $should_switch, $current_action, $subscription, $object
);
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:647
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 );
}