Skip to content
Advertisement

Remove action buttons from WooCommerce Subscriptions dashboard for a specific user role

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!

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement