skip to Main Content

According to this post, custom post type, hide or disable the trash button in publish meta box, this is specifically removing for post. I couldn’t find any post solutions relating to remove ‘Move to trash’ under woocommerce order action. Does this code apply same to removing this button on woocommerce?

function my_custom_admin_styles() {
?>
    <style type="text/css">
      .post-type-inhoud form #delete-action{
           display:none;
       }
     </style>
<?php
}
add_action('admin_head', 'my_custom_admin_styles');

How do I apply this to woocommerce removing ‘move to trash’ button under order action and only specifically for shop manager.

2

Answers


  1. To prevent deleting post/order/custom post is capability per user role. To prevent shop_manager role from deleting orders you must remove that capability. All other solutions will visualy work but someone who knows his stuff still can delete order if he wants. So place the following function in your functions.php

    function wp23958_remove_shop_manager_capabilities() {
        $shop_manager = get_role( 'shop_manager' ); // Target user role
        //List of capabilities which we want to edit
        $caps = array(
            'delete_shop_orders',
            'delete_private_shop_orders',
            'delete_published_shop_orders',
            'delete_others_shop_orders',
        );
        // Remove capabilities from our list 
        foreach ( $caps as $cap ) {
            $shop_manager->remove_cap( $cap );
        }
    }
    add_action( 'init', 'wp23958_remove_shop_manager_capabilities' );
    

    Bonus How to know what capabilities current user have

    function wp32985_check_user_capabilities() {
        $data = get_userdata( get_current_user_id() );
     
        if ( is_object( $data) ) {
            $current_user_caps = $data->allcaps;
            
            // print it to the screen
            echo '<pre>' . print_r( $current_user_caps, true ) . '</pre>';
        }
    }
    add_action( 'init', 'wp32985_check_user_capabilities' );
    
    Login or Signup to reply.
  2. The correct CSS class to target is: .order_actions li #delete-action

    The following will hide it for all backend users (admins, shop managers, etc.):

    add_action( 'admin_head', 'bbloomer_css_wp_admin' );
    
    function bbloomer_css_wp_admin() {
       echo '<style>.order_actions li #delete-action { display: none; }</style>';
    }
    

    If you only wanted to apply this CSS for shop managers (and therefore not for admins for example), you need conditional logic, and specifically you need to use the inbuilt WooCommerce function wc_current_user_has_role():

    add_action( 'admin_head', 'bbloomer_css_wp_admin' );
    
    function bbloomer_css_wp_admin() {
       if ( ! wc_current_user_has_role( 'shop_manager' ) ) return;
       echo '<style>.order_actions li #delete-action { display: none; }</style>';
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search