Limit users to one active subscription in WooCommerce Subscriptions? [closed]

I am in the process of building a membership/subscription based site for a client of mine and they are using woocommerce subscriptions (https://woocommerce.com/products/woocommerce-subscriptions). Now the problem is the client is building a few promo pages which basically allows the user to purchase an upgrade. Now, this is fine but the client only wants a customer to only have one subscription (and associating membership [https://woocommerce.com/products/woocommerce-memberships/]) at any one time.

So the agreed solution is that, on a purchase of any new subscription/product, all other subscriptions should be cancelled. All associated membership deleted/cancelled and only the latest subscription should remain active with its accompanying membership.

So I have tried to build this solution but it is just not working, so any advise/direction would be most welcome!

function wp56908_new_order_housekeeping ($order_id)
{
    $args = array(
        'subscriptions_per_page' => -1,
        'customer_id'            => get_current_user_id(),
    );

    $subscriptions = wcs_get_subscriptions($args);

    foreach ($subscriptions as $subscription) {
        $s_order_id = method_exists( $subscription, 'get_parent_id' ) ? $subscription->get_parent_id() : $subscription->order->id;
        if ($s_order_id != $order_id) {
            $cancel_note="Customer purchased new subscription in order #" . $order_id;
            $subscription->update_status( 'cancelled', $cancel_note );
        }
    }
}

2 Answers
2

Unless I’m misunderstanding you, WC Subscriptions already has this functionality.

Firstly, set your subscription product to be variable or grouped, rather than having multiple individual products.

Set the subscription product to limit purchasing: https://docs.woocommerce.com/document/subscriptions/store-manager-guide/#limit-subscription

Then turn on allow switching: https://docs.woocommerce.com/document/subscriptions/switching-guide/#section-2

Hope that helps

Leave a Comment