【问题标题】:Disabling the editing of the order shipping details by admin in WooCommerce backend管理员在 WooCommerce 后端禁用对订单发货详细信息的编辑
【发布时间】:2020-12-03 12:07:21
【问题描述】:

我们使用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;
}

但我也想禁用更改运输详细信息(姓名、地址等)的功能。

逻辑是,如果尚未发送订单,我会让我们的员工更改订单商品和运输信息,但一旦发送订单,我想禁用它。

【问题讨论】:

    标签: php wordpress woocommerce backend orders


    【解决方案1】:

    没有立即调整的过滤器,因此您可以使用一些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 );
    

    【讨论】:

    • 感谢@7uc1f3r 很好的回答。只是出于好奇,您知道这种使用 jquery 的方法是否是用于禁用订单项编辑的方法?谢谢
    • 编辑图标(订单项)的显示由一个if条件通过php代码if ( $order-&gt;is_editable() )决定,通过wc_order_is_editable过滤器你可以判断这是真还是假。运输详细信息的编辑图标不包含在这种 if 条件中,因此无法通过过滤器挂钩进行编辑,必须使用 jQuery 之类的解决方案
    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 2021-11-03
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多