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

fix(wcs): expire manual subscriptions after on-hold duration #3681

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
* Main class.
*/
class On_Hold_Duration {
/**
* The hook name for the manual subscription expiration scheduled action.
*/
const AS_HOOK = 'newspack_expire_manual_subscription';

/**
* The group name for the manual subscription expiration scheduled action.
*/
const AS_GROUP = 'newspack_expire_manual_subscription';

/**
* Initialize hooks and filters.
*/
Expand All @@ -23,6 +33,9 @@ public static function init() {

add_filter( 'woocommerce_subscription_settings', [ __CLASS__, 'add_on_hold_duration_setting' ], 11, 1 );
add_filter( 'wcs_default_retry_rules', [ __CLASS__, 'maybe_apply_on_hold_duration_rule' ], 99, 1 );
add_action( 'woocommerce_generated_manual_renewal_order', [ __CLASS__, 'maybe_schedule_expiration' ], 10, 2 );
add_action( 'woocommerce_subscription_renewal_payment_complete', [ __CLASS__, 'maybe_unschedule_expiration_on_manual_renewal' ], 10, 2 );
add_action( self::AS_HOOK, [ __CLASS__, 'handle_scheduled_action' ] );
}

/**
Expand Down Expand Up @@ -104,4 +117,43 @@ public static function maybe_apply_on_hold_duration_rule( $retry_rules ) {
}
return $retry_rules;
}

/**
* Conditionally schedule expiration action.
*
* @param int $order_id Order ID.
* @param object $subscription Subscription object.
*/
public static function maybe_schedule_expiration( $order_id, $subscription ) {
if ( 'on-hold' === $subscription->get_status() ) {
$default_grace_period = 7 * DAY_IN_SECONDS; // 7 days, the number of days the retry system normally waits before marking a subscription as expired.
$on_hold_duration = self::get_on_hold_duration() * DAY_IN_SECONDS;
$timestamp = $subscription->get_time( 'next_payment' ) + $default_grace_period + $on_hold_duration;
as_schedule_single_action( $timestamp, self::AS_HOOK, [ $subscription->get_id() ], self::AS_GROUP );
}
}

/**
* Unschedule expiration if scheduled when manual subscription is renewed.
*
* @param \WC_Subscription $subscription The Subscription.
* @param \WC_Order $order The order.
*/
public static function maybe_unschedule_expiration_on_manual_renewal( $subscription, $order ) {
if ( false !== as_has_scheduled_action( self::AS_HOOK, [ $subscription->get_id() ], self::AS_GROUP ) ) {
as_unschedule_action( self::AS_HOOK, [ $subscription->get_id() ], self::AS_GROUP );
}
}

/**
* Handle expiration scheduled action.
*
* @param int $subscription_id Subscription ID.
*/
public static function handle_scheduled_action( $subscription_id ) {
$subscription = wcs_get_subscription( $subscription_id );
if ( $subscription && 'on-hold' === $subscription->get_status() ) {
$subscription->update_status( 'expired' );
}
}
}
Loading