首页 > 解决方案 > 在 WordPress 管理仪表板上删除 WooCommerce 中“添加订单”的子菜单

问题描述

我的 WooCommerce 版本是 4.5.2。

我想删除自定义用户的“添加订单”,使其无法访问wp-admin/post-new.php?post_type=shop_order

我使用具有以下权限的用户角色编辑器创建了一个自定义用户:

这样,用户只能查看现有订单,并单击订单预览以更新为“已完成”。

我尝试使用这个:

remove_submenu_page( 'edit.php?post_type=shop_order', 'post-new.php?post_type=shop_order');

...但订单主菜单变得无法访问。

我在批量订单面板上看到了这篇文章删除或隐藏 woocommerce 上的“添加新”按钮,该按钮使用 CSS 从页面中隐藏了“添加订单”。

我希望有人可以为我指明如何实现我正在寻找的方向。


更新:

基于 7uc1f3r 的回答,这是我的输出

[edit.php?post_type=shop_order] => Array
    (
        [5] => Array
            (
                [0] => Orders 
                [1] => edit_shop_orders 
                [2] => edit.php?post_type=shop_order
            )
            
        [10] => Array
            ( 
                [0] => Add order 
                [1] => edit_shop_orders 
                [2] => post-new.php?post_type=shop_order 
            )
    ) 

使用提供的解决方案,我使用它以便自定义用户无法添加订单和访问 wp-admin/post-new.php?post_type=shop_order:

    unset( $submenu['edit.php?post_type=shop_order'][10][0] );
    unset( $submenu['edit.php?post_type=shop_order'][10][1] );
    unset( $submenu['edit.php?post_type=shop_order'][10][2] );

此外,我应用 CSS 来隐藏管理面板中的“添加顺序”:

    ul.wp-submenu.wp-submenu-wrap {
        display: none !important;
    }

现在看起来像这样:

标签: phpwordpresswoocommercebackenddashboard

解决方案


I'm using WC 4.4.1 & WC 4.6.0 and in both versions there is no possibility to create a new order from the menu.

UPDATE: Due to the output you posted, this should suffice to remove "Order: add new"

function action_admin_menu() {
    global $menu, $submenu;

    // Unset 'Order: add new'
    unset( $submenu['edit.php?post_type=shop_order'][10] );
}
add_action( 'admin_menu', 'action_admin_menu' );

Optional: For "Products: add new" and DEBUGGING you could use

// DEBUG: This displays the complete wordpress admin menu on your dashboard for admin only. (Remove afterwards)
function debug_admin_menus() {
    global $menu, $submenu, $pagenow;
    if ( current_user_can('manage_options') ) {
        if( $pagenow == 'index.php' ) {  // print on dashboard
            echo '<pre>', print_r( $menu, 1 ), '</pre>'; // top level menus
            echo '<pre>', print_r( $submenu, 1 ), '</pre>'; // submenus
        }
    }
}
add_action( 'admin_notices', 'debug_admin_menus' );

function action_admin_menu() {
    global $menu, $submenu;

    // Unset 'Products: add new'
    unset( $submenu['edit.php?post_type=product'][10] );
}
add_action( 'admin_menu', 'action_admin_menu' );

Related:


推荐阅读