1

I'm looking to bring on a virtual assistant to help manage support tickets. This virtual assistant will need to have reading access to limited areas of WooCommerce (Subscriptions).

I'm using 'user role editor' to remove all capabilities that are not needed. Unfortunately, a single capability (edit_shop_orders) gives access to functions that I do not want the agent to have access to. I'm forced to give the agent this capability to have access to the subscription menu in the back end.


What I am trying to do:

Remove the 'Suspend' and 'Cancel' button access from a particular user role (va_support)

enter image description here



My current code(not working):

function change_va_support_role(){
    global $wp_roles;
    $wp_roles->remove_cap( 'va_support', 'suspend_subscriptions' );
    $wp_roles->remove_cap( 'va_support', 'cancel_subscriptions' );

}
add_action('init', 'change_va_support_role');

I am assuming I've entered incorrect capabilities, but I can't seem to find them anywhere.
I understand that I can probably easily hide these buttons with CSS, but that can just be reversed and will not be user role dependent. I'm open to solving this issue another way if there is!

mujuonly
  • 11,370
  • 5
  • 45
  • 75
cactusboat
  • 778
  • 2
  • 7
  • 15

2 Answers2

0

Using Javascript / jQuery the following will remove all "hovered" action buttons, for a specific user role, on the subscriptions admin dashboard from the status column:

add_action( 'admin_footer', 'admin_dashboard_subscriptions_filter_callback' );
function admin_dashboard_subscriptions_filter_callback() {
    global $pagenow, $post_type;

    $targeted_user_role = 'va_support'; // <== Your defined user role slug

    // Targeting subscriptions admin dashboard for specific user role
    if( $pagenow === 'edit.php' && $post_type === 'shop_subscription' 
    && current_user_can( $targeted_user_role ) ) :
    ?>
    <script>
    jQuery(function($){
        $('td.column-status > div.row-actions').each(function() {
            $(this).remove();
        });
    });
    </script>
    <?php
    endif;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    Amazing, works perfectly! Thank you for taking the time to answer. If in the near future I need help with a larger project, I will reach out :) - (ps i cannot give you bounty for anther 19 hours) – cactusboat Apr 27 '20 at 16:20
  • 1
    This will only hide the action links. If someone accesses the link directly, it will work :) – mujuonly Apr 28 '20 at 05:33
  • 2
    Can't say I'd much hesitate to use this solution in a pinch, but it's kind of cheating ;) – CK MacLeod Apr 28 '20 at 07:06
  • 2
    @CKMacLeod Sometimes cheating is better than nothing :) – LoicTheAztec Apr 28 '20 at 07:17
  • Hi @LoicTheAztec I tried to contact you via your 'contact me' link in your profile, not sure if you received my message? – cactusboat Nov 20 '20 at 08:34
-1

As of now, there is no filter where handling this action trigger. You may add a filter on your own for handling this.

/woocommerce-subscriptions/includes/admin/class-wcs-admin-post-types.php inside this function around Line# 330 - public function parse_bulk_actions()

/*
 * Use below hook for handling custom user role permission for action from subscription listing page
 */
$can_change_status = apply_filters('wcs_can_change_subscription_status', true, $subscription_ids);
if(!$can_change_status){
    return;
}

In theme, you can check the current user has the role then return true/false accordingly. You may be aware that when updating the plugin, the changes will be lost. But as far as checking the code, there is no filter just before triggering these functions.

add_filter( 'wcs_can_change_subscription_status', 'alter_can_change_subscription_status' );

function alter_can_change_subscription_status( $can_change_status ) {

    $user = wp_get_current_user();
    if ( in_array( 'va_support', (array) $user->roles ) ) {
        //The user has the "va_support" role
        return false;
    }
    return  $can_change_status;
}
mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • Hi, thanks for your input! I've just tested your code by placing it under 'public function parse_bulk_actions()'. I then added 'can_change_status' as a custom capability in 'user role editor' and left it unchecked. Unfortunately I am not seeing a change! Am I doing something wrong? – cactusboat Apr 25 '20 at 12:54
  • @cactusboat - Add the second snippet in functions.php – mujuonly Apr 25 '20 at 17:56
  • Hey @mujuonly, I just tried and it still doesn't seem to work :( I really appreciate you putting time into answering my question! – cactusboat Apr 25 '20 at 18:07
  • 1
    @cactusboat - try just return false in the start of the filter function if it works. then you we can confirm the issue is with role checking, otherwise the issue is with the position of the code u added in the file mentioned – mujuonly Apr 25 '20 at 18:10
  • I replaced true with false in the first code, it did nothing. I also tried repositioning within 'public function parse_bulk_actions()'. Nothing has changed, VA Support can still see cancel/suspend – cactusboat Apr 25 '20 at 18:42
  • I think the WordPress way to handle this situation would more likely be to fork than to hack the plugin, assuming that removing and replacing the function (or getting the developer to add the filter!) isn't practical. – CK MacLeod Apr 28 '20 at 07:05
  • @CKMacLeod The other solution is just hiding the button but not restricting the functionality. I suggest either forking or adding a filter in the plugin directly ( as the OP know what he's doing ) – mujuonly Apr 28 '20 at 07:25