In woocommerce subscription while switching subscription how to keep active previous old subscription keep active
In woocommerce subscription while switching subscription how to keep active previous old subscription keep active
In woocommerce subscription while switching subscription how to keep active previous old subscription keep active for maximum 3 susbcription orders and in my plugin i had customize like if user downgrade susbcription then the subscription will go "on-hold" status for 1 month , after 1 month the status changed to "processing" and "active" and if user upgrade the subscription the status will go on processing and active , the problem is when subscription switch to upgrade or downgrade the previous susbcription status turns to cancel problem , i want previous subscription to active, Thanks in advance .
Regards
I'd tried this code for upgrading and downgrading susbcirption.
add_action('woocommerce_order_status_completed', 'updating_order_status_with_subscription_changes');
function updating_order_status_with_subscription_changes($order_id) {
$order = wc_get_order($order_id);
if (wcs_order_contains_subscription($order)) {
$subscriptions = wcs_get_subscriptions_for_order($order_id);
foreach ($subscriptions as $subscription_id => $subscription) {
$subscription_change = check_subscription_change($subscription);
if ($subscription_change == 'downgrade') {
// Set the subscription to 'on-hold' if it's a downgrade
$subscription->update_status('on-hold');
// Set the order status to 'on-hold' as well
$order->update_status('on-hold', 'Subscription downgraded, setting order to on-hold.');
// Schedule event to reactivate subscription
$date_one_minute_later = date('Y-m-d H:i:s', strtotime('+1 minutes'));
wp_schedule_single_event(strtotime($date_one_minute_later), 'reactivate_subscription', array('subscription_id' => $subscription_id));
// Schedule event to update order status to 'processing'
wp_schedule_single_event(strtotime($date_one_minute_later), 'update_order_status_to_processing', array('order_id' => $order_id));
} elseif ($subscription_change == 'upgrade') {
// Keep or set the subscription to 'active' if it's an upgrade
$subscription->update_status('active');
}
// No specific action for no change
}
}
}
function check_subscription_change($subscription) {
// Logic to determine if the subscription is a downgrade or an upgrade
$user_id = $subscription->get_user_id();
$current_total = $subscription->get_total();
// Get all subscriptions for the user
$user_subscriptions = wcs_get_users_subscriptions($user_id);
foreach ($user_subscriptions as $user_subscription) {
if ($user_subscription->get_id() != $subscription->get_id()) {
if ($user_subscription->get_total() < $current_total) {
return 'downgrade'; // Found a previous subscription that was more expensive
} elseif ($user_subscription->get_total() > $current_total) {
return 'upgrade'; // Found a previous subscription that was less expensive
}
}
}
return 'no_change'; // No significant change found
}
// Handler to reactivate the subscription
add_action('reactivate_subscription', 'reactivate_subscription_handler');
function reactivate_subscription_handler($subscription_id) {
$subscription = wcs_get_subscription($subscription_id);
if ($subscription && $subscription->has_status('on-hold')) {
$subscription->update_status('active');
}
}
// Handler to update order status to 'processing'
add_action('update_order_status_to_processing', 'update_order_status_to_processing_handler');
function update_order_status_to_processing_handler($order_id) {
$order = wc_get_order($order_id);
if ($order && $order->has_status('on-hold')) {
$order->update_status('processing', 'Auto-updating order status to processing after subscription downgrade.');
}
}
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand