在Woocommerce中将订单更新为具有多个自定义状态的自定义状态

时间:2019-01-23 13:03:30

标签: php wordpress woocommerce hook-woocommerce orders

在Woocommerce中,我无法将订单设置为以下自定义订单状态之一:'wc-order-waiting-to-abort'。我创建了3个其他自定义状态,它们都可以正常工作,并且可以设置为订单。

对于这些自定义状态,我做了完全相同的事情。

奇怪的是,自定义状态显示在WooCommerce的下拉列表中,我可以选择它,但是当我按“保存订单”时,该状态不会保存,而是保持旧状态。当我选择其他自定义状态并按保存时,状态已成功保存。

没有错误日志,没有警告,什么都没有。真奇怪

这是我的代码:

add_action( 'init', 'register_new_order_states_and_status' );
function register_new_order_states_and_status() {

    //Bestellung in Bearbeitung
    register_post_status( 'wc-order-in-progress', array(
        'label'                     => 'Bestellung in Bearbeitung',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Bestellung in Bearbeitung <span class="count">(%s)</span>', 'Bestellung in Bearbeitung <span class="count">(%s)</span>' )
    ) );

    //Abbruchbestätigung ausstehend
    register_post_status( 'wc-order-waiting-to-abort', array(
        'label'                     => 'Abbruchbestätigung ausstehend',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Abbruchbestätigung ausstehend <span class="count">(%s)</span>', 'Abbruchbestätigung ausstehend <span class="count">(%s)</span>' )
    ) );

    //Bestellung abgeschlossen
    register_post_status( 'wc-order-finished', array(
        'label'                     => 'Bestellung abgeschlossen',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Bestellung abgeschlossen <span class="count">(%s)</span>', 'Bestellung abgeschlossen <span class="count">(%s)</span>' )
    ) );

    //Bestellung abgenommen
    register_post_status( 'wc-order-accepted', array(
        'label'                     => 'Bestellung abgenommen',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Bestellung abgenommen <span class="count">(%s)</span>', 'Bestellung abgenommen <span class="count">(%s)</span>' )
    ) );
}

/**
 * Add new order states to WooCommerce
 */
add_filter( 'wc_order_statuses', 'add_new_order_states' );
function add_new_order_states( $order_states ) {

    $new_order_states = array();

    foreach ( $order_states as $key => $status ) {

        $new_order_states[ $key ] = $status;

        if ( $key === 'wc-completed' ) {
            $new_order_states['wc-order-in-progress']      = 'Bestellung in Bearbeitung';
            $new_order_states['wc-order-waiting-to-abort'] = 'Abbruchbestätigung ausstehend';
            $new_order_states['wc-order-finished']         = 'Bestellung abgeschlossen';
            $new_order_states['wc-order-accepted']         = 'Bestellung abgenommen';
        }
    }

    return $new_order_states;
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

“订单”自定义状态键太长,应该更简单,更短,因此,我再次回顾了您的代码,重命名了您的“自定义订单状态”键(段)并使其更加紧凑:

// Utility function for custom order status data array (key/label pairs)
function get_custom_order_statuses(){
    return array(
        'wc-in-progress'        => __('Bestellung in Bearbeitung'),     // Bestellung in Bearbeitung
        'wc-waiting-cancel'     => __('Abbruchbestätigung ausstehend'), // Abbruchbestätigung ausstehend
        'wc-finished'           => __('Bestellung abgeschlossen'),      // Bestellung abgeschlossen
        'wc-accepted'           => __('Bestellung abgenommen'),         // Bestellung abgenommen
    );
}


// Register custom Order statuses
add_action( 'init', 'register_custom_order_statuses' );
function register_custom_order_statuses() {
    // Loop through custom order statuses array (key/label pairs)
    foreach( get_custom_order_statuses() as $key => $label ) {
        register_post_status( $key, array(
            'label'                     => $label,
            'public'                    => true,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop( $label . ' <span class="count">(%s)</span>', $label . ' <span class="count">(%s)</span>' )
        ) );
    }
}

// Add custom Order statuses
add_filter( 'wc_order_statuses', 'add_custom_order_statuses', 10, 1 );
function add_custom_order_statuses( $order_statuses ) {
    $sorted_order_statuses = array(); // Initializing

    foreach ( $order_statuses as $key => $label ) {
        $sorted_order_statuses[ $key ] = $label;

        if ( $key === 'wc-completed' ) {
            // Loop through custom order statuses array (key/label pairs)
            foreach( get_custom_order_statuses() as $custom_key => $custom_label ) {
                $sorted_order_statuses[$custom_key] = $custom_label;
            }
        }
    }

    return $sorted_order_statuses;
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。