How to disable the "Upgrade or Downgrade" button in "My account" of WooCommerce Subscriptions

I have a Wordpress/WooCommerce webshop and I use WooCommerce Subscriptions. I want to disable the "Upgrade or Downgrade" button in My account > My Subsccriptions for users which have a specific user role. I found 2 pieces of php script but I don't know how to combine these two.

The first is to check if a user has a specific role:

/**
 * Check user has specific role
 *
 * @param string $role
 * @param int $user_id
 * @return bool
 */
function is_user_has_role( $role, $user_id = null ) {
    if ( is_numeric( $user_id ) ) {
        $user = get_userdata( $user_id );
    }
    else {
        $user = wp_get_current_user();
    }
    if ( !empty( $user ) ) {
        return in_array( $role, (array) $user-roles );
    }
    else
    {
        return false;
    }
}

If the user has the user role 'subscriber_plus' the "Upgrade or Downgrade" button should be disabled/removed. For that I found this php script:

/**
 * Remove the "Change Payment Method" button from the My Subscriptions table.
 *
 * This isn't actually necessary because @see eg_subscription_payment_method_cannot_be_changed()
 * will prevent the button being displayed, however, it is included here as an example of how to
 * remove just the button but allow the change payment method process.
 */

function eg_remove_my_subscriptions_button( $actions, $subscription ) {
    foreach ( $actions as $action_key = $action ) {
        switch ( $action_key ) {
//          case 'change_payment_method':   // Hide "Change Payment Method" button?
//          case 'change_address':      // Hide "Change Address" button?
            case 'switch':          // Hide "Switch Subscription" button?
//          case 'resubscribe':     // Hide "Resubscribe" button from an expired or cancelled subscription?
//          case 'pay':         // Hide "Pay" button on subscriptions that are "on-hold" as they require payment?
//          case 'reactivate':      // Hide "Reactive" button on subscriptions that are "on-hold"?
//          case 'cancel':          // Hide "Cancel" button on subscriptions that are "active" or "on-hold"?
                unset( $actions[ $action_key ] );
                break;
            default: 
                error_log( '-- $action = ' . print_r( $action, true ) );
                break;
        }
    }
    return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'eg_remove_my_subscriptions_button', 100, 2 );

I can't figure it out how to combine these scripts to make this work. Hope my question is clear.

My Wordpress version is: 5.1.1 WooCommerce Subscriptions version: 2.5.2

Topic woocommerce-offtopic user-roles subscription Wordpress

Category Web


I used the following code to remove the action to prevent the code from running. I think it's cleaner than using the filter to return an empty string for the switch link.

remove_action('woocommerce_order_item_meta_end', array('WC_Subscriptions_Switcher', 'print_switch_link'), 10, 3);

I have found/made the code that works. Maybe it helps somebody else.

/**
* Remove the "Upgrade or Downgrade" button from the My Subscription table if user role is "subscriber_plus".
*/
add_filter('woocommerce_subscriptions_switch_link', 'remove_switch_button', 10, 4);
function remove_switch_button($switch_link, $item_id, $item, $subscription) {
$user = wp_get_current_user();
if ( in_array( 'subscriber_plus', (array) $user->roles ) ) {
    return '';
}
return $switch_link;

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.