WooCommerce Change Role if Subscription is Purchased then Change back if canceled or expired
03:44 13 Nov 2025

I need help.

I'm using the WooCommerce Plugin along with the "Subscriptions For WooCommerce" and I'm not seeing how to set this up.

If a customer purchases a monthly subscription, I want the Role to change to "Monthly Subscriber" and then if they cancel or it expires, the Role Changes back to Customer.

Will this work?

add_action( 'subscription_expired', 'my_function', 10, 2 );

function my_function( $user_id, $subscription_key ) {
    $sub= wcs_get_subscription_from_key( $subscription_key );
    // do something
}
php wordpress function woocommerce hook-woocommerce