如何禁用WooCommerce订阅的“我的帐户”中的“升级或降级”按钮

时间:2019-03-14 作者:Frotmans

我有一个Wordpress/WooCommerce网站商店,我使用WooCommerce订阅。我想为具有特定用户角色的用户禁用“我的帐户”>“我的子描述”中的“升级或降级”按钮。我找到了两段php脚本,但我不知道如何将这两段结合起来。

第一个是检查用户是否具有特定角色:

/**
 * 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;
    }
}
如果用户具有用户角色“subscriber\\u plus”,则应禁用/删除“升级或降级”按钮。为此,我找到了以下php脚本:

/**
 * 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 );
我不知道如何组合这些脚本来实现这一点。希望我的问题很清楚。

我的Wordpress版本是:5.1.1WooCommerce订阅版本:2.5.2

2 个回复
SO网友:Frotmans

我已经找到/制作了有效的代码。也许这对其他人有帮助。

/**
* 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;

SO网友:Andrew Schultz

我使用以下代码删除了阻止代码运行的操作。我认为这比使用过滤器为交换机链接返回空字符串更干净。

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

相关推荐

Subscription list function

我创建了一个功能,可以自动将具有参与者角色的用户添加到一个列表中,在该列表中,他们将通过电子邮件接收论坛(修改后的mingleforum插件)更新。我的问题是,“is\\u player\\u subscribed()”函数调用的函数似乎工作不正常,因为它每次用户注册时都会有效地添加整个列表。add_action( \'user_register\', \'call_forum_subscribe_member_player\' ); function forum_subscribe_m