我有一个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
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;