簡體   English   中英

禁止管理員在 WooCommerce 后端編輯訂單發貨詳情

[英]Disabling the editing of the order shipping details by admin in WooCommerce backend

我們使用wc_order_is_editable鈎子來禁用對某些訂單狀態的后端訂單項目的編輯。

add_filter( 'wc_order_is_editable', 'wc_make_orders_editable', 10, 2 );
function wc_make_orders_editable( $is_editable, $order ) {
    if ( $order->get_status() == 'completed' ) {
        $is_editable = false;
    }

    return $is_editable;
}

但我也想禁用更改運輸詳細信息(姓名、地址等)的功能。

邏輯是,如果尚未發送訂單,我會讓我們的員工更改訂單項目和運輸信息,但是一旦發送訂單,我想禁用它。

沒有立即調整它的過濾器,因此您可以使用一些jQuery來隱藏編輯圖標。

  • 僅在訂單編輯頁面上
  • 檢查用戶角色、管理員
  • 基於一個或多個訂單狀態

重要提示:因為“帳單詳細信息”和“運輸詳細信息”之間沒有直接區別,所以包含H3選擇器標題的一部分

$( "h3:contains('Shipping') .edit_address" );

其中“運輸”可能需要替換為您使用的語言中的標題。


所以你得到:

function action_admin_footer () {
    global $pagenow;
    
    // Only on order edit page
    if ( $pagenow != 'post.php' || get_post_type( $_GET['post'] ) != 'shop_order' ) return;
    
    // Get current user
    $user = wp_get_current_user();

    // Safe usage
    if ( ! ( $user instanceof WP_User ) ) {
        return;
    }

    // In array, administrator role
    if ( in_array( 'administrator', $user->roles ) ) {
        // Get an instance of the WC_Order object
        $order = wc_get_order( get_the_id() );
        
        // Is a WC_Order
        if ( is_a( $order, 'WC_Order' ) ) {
            // Get order status
            $order_status = $order->get_status();
            
            // Status in array
            if ( in_array( $order_status, array( 'pending', 'on-hold', 'processing' ) ) ) {
                ?>
                <script>
                jQuery( document ).ready( function( $ ) {
                    // IMPORTANT: edit H3 tag contains 'Shipping' if necessary
                    $( "h3:contains('Shipping') .edit_address" ).hide();
                });
                </script>
                <?php
            }
        }
    }
}
add_action( 'admin_footer', 'action_admin_footer', 10, 0 );

暫無
暫無

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

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