簡體   English   中英

防止用戶角色更改 woocommerce 訂單狀態

[英]Prevent User Role from changing woocommerce order status

我們想阻止商店經理更改訂單狀態,我們在下面的鏈接中找到了幫助限制用戶角色僅更改 Woocommerce 中的某些訂單狀態

但是這里的問題是它把某個角色(店長)限制在了一些訂單狀態,我們需要完全拒絕店長改變訂單狀態,而不是把它限制在一些訂單狀態。

另外,片斷,我們提到從批量操作刪除的訂單狀態下拉與此訂單的詳細信息: https://prnt.sc/mpfl3b ,我們需要從這里快速行動柱太刪除狀態https://開頭snipboard .io/B6SYHb.jpg

簡單地,我們嘗試讓商店經理在他嘗試從批量、訂單詳細信息頁面或操作欄中更改訂單狀態時,發現沒有訂單狀態可供選擇更改或完全禁用。

此致

正如您在示例代碼中看到的,狀態的條件在 if 語句中確定,因為您想無限制地應用它,只需刪除該 if 語句並返回空數組

ps; 如果您將我的答案標記為解決方案,那么如果您還沒有這樣做,那么也投票給@LoicTheAztec原始答案,因為他的代碼幾乎包含了解決方案。

// Admin orders list: bulk order status change dropdown
function filter_dropdown_bulk_actions_shop_order( $actions ) {
    // Targeting shop_manager
    if( current_user_can( 'shop_manager' ) ) {
        $actions = (array) null;
    }

    return $actions;
}
add_filter( 'bulk_actions-edit-shop_order', 'filter_dropdown_bulk_actions_shop_order', 20, 1 );

// Admin orders list: quick action
function filter_order_actions( $actions, $order ) {
    // Targeting shop_manager
    if( current_user_can( 'shop_manager' ) ) {
        $actions = (array) null;
    }

    return $actions;
}
add_filter( 'woocommerce_admin_order_actions', 'filter_order_actions', 10, 2 );

// Admin order pages: order status dropdown
function filter_order_statuses( $order_statuses ) { 
    global $post, $pagenow;

    if( $pagenow === 'post.php' || $pagenow === 'post-new.php' ) {
        // Get ID
        $order_id = $post->ID;

        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );

        // TRUE
        if ( $order ) { 
            // Get current order status
            $order_status = 'wc-' . $order->get_status();

            // New order status
            $new_order_statuses = array();

            foreach ($order_statuses as $key => $option ) {
                // Targeting "shop_manager"
                if( current_user_can('shop_manager') && $key == $order_status ) {
                    $new_order_statuses[$key] = $option;
                }
            }

            if( sizeof($new_order_statuses) > 0 ) {
                return $new_order_statuses;
            }
        }
    }
    return $order_statuses;
}
add_filter('wc_order_statuses', 'filter_order_statuses', 10, 1 );

//由於建議的答案顯然會導致一些新問題並且在其他幾種情況下無法解決原始問題,因此可以選擇根據用戶類型隱藏元素,如下所示 - 這有點麻煩,但是可能服務於:

首先,加載僅適用於商店經理的管理樣式表:

/**
 * SHOP MANAGER STYLES 
 * Front (Optional) and Back End stylesheet 
 * Style interface for users logged in with'shop_manager' role
 * Add to theme functions.php
 */
add_action('admin_enqueue_scripts', 'shop_manager_styles');
//if front end stylesheet needs to be added to cover admin bar:
//add_action('wp_enqueue_scripts', 'shop_manager_styles' ) ; 

function shop_manager_styles() {  

    $user = wp_get_current_user() ;

    //uncomment following and remove next if not confined to admin  
    //if ( $user && in_array( 'shop_manager', $user->roles )  ) { 
    if ( in_array( 'shop_manager', $user->roles ) ) {

        //time() as stylesheeet version to help bust caching - may not be necessary but doesn't hurt:
        wp_enqueue_style( 
            'shop_manager_styles', get_stylesheet_directory_uri() 
            . '/css/shop_manager_styles.css', array(), time() 
        ); 

    } 

}

...以及完全隱藏訂單狀態標簽和菜單的 css,以及 shop_order 子頁面中的相關列:

/** HIDE ORDER STATUS LABEL, SELECTION MENU IN ORDER EDIT
  * AND RELATED COLUMNS IN shop_order SUB-PAGE
  */
.wc-order-status, 
.column-order_status,
.column-wc_actions {
    display: none;
}

您可以將其保存在新 shop_manager_styles.css 中的主題 css 文件夾中。

現在,您可能需要向商店經理顯示訂單狀態,而他們無法對其進行編輯。 如果還(甚至更多)雜亂無章,那也可以使用 CSS。 可能是您的安裝中有其他特性會阻止上述代碼或它的最小定制變體工作,但是,即使它比通過函數刪除選項更不干凈,這種事情通常會在緊要關頭工作。

(編輯以提供在前端添加樣式表的選項- 以防相關選項出現在管理欄中,否則無需排隊額外的非管理腳本。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM