2

I am in the process of building a membership / subscription based site for a client of mine and we are using Woocommerce Subscriptions and Woocommerce Memberships plugin.

Now the problem is the my client is building a few promo pages which basically allows the user to purchase an upgrade. This is fine but my client only wants one unique subscription by customer (with its associated membership).

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.

What I have tried:

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 );
        }
    }
}
add_action( 'woocommerce_thankyou', 'wp56908_new_order_housekeeping',  10, 1  );
Johan Rheeder
  • 319
  • 4
  • 23

2 Answers2

0

This is the support email I got from WooCommerce regarding the issue with the function wcs_get_subscriptions().

Thanks for contacting support and report us this issue!

I've tried it in a local install and I confirm that this seems to be a bug! I've already reported it to our development team and a patch should be included in next updates. By now, if you're in a real hurry and want to get it working, you could search for this code (in wcs-functions.php line 483):

// We need to restrict subscriptions to those which contain a certain product/variation if ( ( 0 != $args['product_id'] && is_numeric( $args['product_id'] ) ) || ( 0 != $args['variation_id'] && is_numeric( $args['variation_id'] ) ) ) { $query_args['post__in'] = wcs_get_subscriptions_for_product( array( $args['product_id'], $args['variation_id'] ) ); }

And replace it with something like:

// We need to restrict subscriptions to those which contain a certain product/variation if ( ( 0 != $args['product_id'] && is_numeric( $args['product_id'] ) ) || ( 0 != $args['variation_id'] && is_numeric( $args['variation_id'] ) ) ) { $prod_args = array( $args['product_id'], $args['variation_id'] ); $prod_args = array_filter($prod_args, function($prod_args) { return ($prod_args !== 0); }); $query_args['post__in'] = wcs_get_subscriptions_for_product( $prod_args ); }

Please take in mind that this is not the ideal solution because you'll be modifying our plugin's code directly and these changes can be lost after updating, but if you feel comfortable with code and want to take the risk as a temporary workaround until we add the proper patch in our plugin, you can do it. ;)

Let me know if you need any help with this!

Thanks, Bernat

Andrew Schultz
  • 4,092
  • 2
  • 21
  • 44
0

There's a new setting in the WC Subscriptions plugin which lets you limit to one active subscription. You can find more information here: https://woocommerce.com/document/subscriptions/store-manager-guide/#:~:text=Limit%20subscriptions,the%20subscription%20from%20the%20dropdown.

The end user will get the message: “You have a subscription to this product. Choosing a new subscription will replace your existing subscription.”

Mariano J. Ponce
  • 174
  • 1
  • 2
  • 9