woocommerce set user role on membership code example

Example: woocommerce subscriptions custom user rolde

add_action( 'woocommerce_subscription_status_expired', 'change_user_role_on_subscription_expired', 10, 1 );
function change_user_role_on_subscription_expired( $subscription ) {
    // Get WP_User Object from subscription
    $user = new WP_User($subscription->get_user_id());

    if ( in_array('agencia_pro', $user->roles) ) {
        $user->remove_role( 'agencia_pro' );
        $user->add_role( 'agencia' );
    }
    elseif ( in_array('agencia_pro_plus', $user->roles) ) {
        $user->remove_role( 'agencia_pro_plus' );
        $user->add_role( 'agencia' );
    }
    elseif ( in_array('talento_pro', $user->roles) ) {
        $user->remove_role( 'talento_pro' );
        $user->add_role( 'talento' );
    }
    elseif ( in_array('talento_pro_plus', $user->roles) ) {
        $user->remove_role( 'talento_pro_plus' );
        $user->add_role( 'talento' );
    }
}

Tags:

Php Example