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