在 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" );

其中 'shipping' 可能需要替换为您使用的语言的标题。


所以你得到:

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 );