首页 > 解决方案 > 在 WooCommerce 中使用自定义订单状态时允许订单完全可编辑

问题描述

在使用自定义状态时,我在订单中的编辑数量存在错误。编辑箭头似乎消失了?

在注册自定义订单状态时,我还可以包含其他值吗?

这是我的代码:

// Register new status 'To order' 
function register_new_on_hold_order_status2() {
    register_post_status( 'wc-to-order', array(
        'label'                     => 'To order',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'To order (%s)', 'To order (%s)' )
    ) );
}
add_action( 'init', 'register_new_on_hold_order_status2' );

// Add to list of WC Order statuses
function add_on_hold_new_to_order_statuses2( $order_statuses ) {
 
    $new_order_statuses = array();
 
    // add new order status after processing
    foreach ( $order_statuses as $key => $status ) {
 
        $new_order_statuses[ $key ] = $status;
 
        if ( 'wc-processing' === $key ) {   // Here we Define after which to be added
            $new_order_statuses['wc-to-order'] = 'To order';
        }
    }
 
    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_on_hold_new_to_order_statuses2' );

在此处输入图像描述

标签: wordpresswoocommercebackendhook-woocommerceorders

解决方案


这不是错误,某些订单状态(例如processing不允许编辑订单)。要改变这一点,你可以使用wc_order_is_editable钩子

所以你得到:

function filter_wc_order_is_editable( $editable, $order ) {
    // Compare
    if ( $order->get_status() == 'your-status' ) {
        $editable = true;
    }
    
    return $editable;
}
add_filter( 'wc_order_is_editable', 'filter_wc_order_is_editable', 10, 2 );

注意:在注册此答案 第二部分中应用的自定义订单状态时使用woocommerce_register_shop_order_post_statuses 相反init


推荐阅读