简体   繁体   中英

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

We use the wc_order_is_editable hook to disable the editing of the order items on the backend for some order statuses.

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

But i wanted to disable the ability to change the shipping details (Name, Address, etc.) as well.

The logic is that if an order isn't sent already i let our staff change the order items and the shipping info but once the order is sent i want to disable it.

There is not immediately a filter to adjust this, so you could use some jQuery , to hide the edit icon.

  • Only on order edit page
  • Checks for user role, administrator
  • Based on one or more order statuses

Important: Because no direct distinction is made between "billing details" and "shipping details" contains the H3 selector a part of the title

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

Where 'shipping' may need to be replaced by the title in the language that you use.


So you get:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM