'WooCommerce Subscriptions: Remove role on cancelled subscription

I am using a third-party plugin to assign a role to a user when they purchase a subscription - there are two different roles depending on purchase (silver, gold)

However, if they cancel that subscription they keep the role.

How do I remove the role when they cancel as I don't want them to keep that role.



Solution 1:[1]

add_action('woocommerce_subscription_status_cancelled', 'wcs_maybe_remove_role', 10, 1);

function wcs_maybe_remove_role($subscription) {
    $user_id = $subscription->get_user_id();
    $user = new WP_User($user_id);
    $user->remove_role('silver');
    $user->remove_role('gold');
}

Solution 2:[2]

For everyone coming to this post, this feature is already built-in under WooCommerce/Settings/Subscriptions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mujuonly
Solution 2 mythos