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

Question

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 Faisal Naseer 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

0
, , Frotmans 11 months 0 Answers 103 views 0

Leave an answer