Home > front end >  Enable and disable 'edit' on orders for shop manager (woocommerce)
Enable and disable 'edit' on orders for shop manager (woocommerce)

Time:09-30

I'm currently using the woocommerce-readonly-role this plugin to disable the ability to edit for a specific user where they only are able to view orders instead of editing just for shipping purposes in woocommerce. However, I need to enable some of the buttons to be editable, and I actually have some other features/custom code in the orders and need to enable it to be editable. Hence, in the current plugin I'm using, no matter which features I've added on, it is still unable to edit as I assuming the plugin is disabling the whole 'clickable' button on the order page regardless of additional features I added into order page.

So, I'm planning to be selective on which button is not accessible for the specific user. The main issue is how do I disable edit (only enable readonly) more button / field edit including for shop manager only as below: You image reference may view from here: https://snipboard.io/AtOWBY.jpg

  1. Disable Add Order (I was following this and added to function Remove or hide "add new" button on woocommerce on bulk order panel using the code below

add_filter( 'woocommerce_register_post_type_shop_order','your_function_name' );
function your_function_name($fields) {
        $fields['capabilities'] = array(
            'create_posts' => false,
          );
        return $fields;
    }

  • it works in admin role, but how can I amend the function which gets executed ONLY for "shop manager" role )
  1. Disable Date created (I was following this WooCommerce: Disable Date on Edit Orders - I've tried this code by using a plugin 'Simple Custom CSS and JS' and add accordingly using the code below.

/**
 * Enqueue a script in the WordPress admin, excluding edit.php.
 *
 * @param int $hook Hook suffix for the current admin page.
 */
function wpdocs_selectively_enqueue_admin_script( $hook ) {
    // if ( 'edit.php' != $hook ) {
    //     return;
    // }
    wp_enqueue_script( 'my_custom_woocommerce_script', get_template_directory_uri() . '/woocommerce/assets/meta-boxes-order.js', array(), '1.0' );
}
add_action( 'admin_enqueue_scripts', 'wpdocs_selectively_enqueue_admin_script' );

jQuery(document).ready(function(){
    alert("ok"); // only for testing purpose that this file is loaded
    jQuery(".order_data_column_container .order_data_column p:eq(0) input").attr('disabled', true);
});

However, it does not work and how can I amend the function which gets executed ONLY for "shop manager" role )

I was searching a lot for below too, but I can't find the file that I have to change for below, where it only can view and non editable.

  1. Disable Customer

  2. disable product link (only able to view product & its SKU instead of clicking redirect to edit product page)

  3. Disable Order Action

  4. Disable custom field

  5. Disable Add New Custom Field

  6. Disable downloadable permission

CodePudding user response:

Use get_current_user(), this way you can get user object then check user's role from the object and if it is shop manager then apply your code as you mentioned.

add_filter( 'woocommerce_register_post_type_shop_order','your_function_name' );
function your_function_name($fields) {
    $user = wp_get_current_user();
    if ( in_array( 'shop_manager', (array) $user->roles ) ) {
        $fields['capabilities'] = array(
            'create_posts' => false,
        );
        return $fields;
    } else {
        return $fields;
    }
}
  • Related