1

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 );
        }
    }
}
3

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

1

I was facing this problem and I check if a user if the user has a active subscription before he can a something to the cart.

There is a hook called woocommerce_add_to_cart_validation.

So you can add a filter on that like this:

add_filter( 'woocommerce_add_to_cart_validation', 'check_subscriptions', 10, 2 );

and than check if the user has active subscriptions like this:

$user_id = get_current_user_id();

$active_subscriptions = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_subscription',
'post_status' => 'wc-active',

) );
if(!empty($active_subscriptions)) return true;
else return false;

I wrote a little blog article about his that is easy to read for beginners: http://robinhenniges.com/woocommerce-subscription-allow-only-one-active-subscription/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.