1
votes

In Woocommerce, I'm not able to set an order to one of my custom order statuses: 'wc-order-waiting-to-abort'. I've created 3 other custom statuses and all of them work fine and can be set to an order.

I've done the exact same thing for those custom status.

The strange thing is that the custom status appears in the dropdown list in WooCommerce, I can select it but when I press Save Order the status isn't saved and stays at the old status. When I select the other custom status and press save the status is successfully saved.

There are no error logs, no warnings, nothing. This is so strange.

Here is my code:

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

Any help is appreciated.

1
You should give all the related code with your 4 custom order statuses as there is something that is interacting somewhere…LoicTheAztec
@LoicTheAztec Added the other ones.Mr. Jo
I can't understand whats the problem. When I select the status and save the order the status don't gets set to the order. I've deleted the logs and removed everything and tried it again but still the same result. So strange mate!Mr. Jo

1 Answers

1
votes

The Order custom status keys are too long and should be simpler and shorter, so I have revisited a bit your code, renaming your custom order statuses keys (slugs) and make it more compact:

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

Code goes in function.php file of your active child theme (or active theme). Tested and works.