Search code examples
phpwordpresswoocommercehook-woocommercewoocommerce-subscriptions

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

  • 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');
    }