skip to Main Content

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!

2

Answers


  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;
    }
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search