php 在Woocommerce中更新订单到自定义状态,具有多个自定义状态

qv7cva1a  于 2023-01-29  发布在  PHP
关注(0)|答案(2)|浏览(169)

在Woocommerce中,我无法将订单设置为我的自定义订单状态之一:我已经创建了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;
}

任何帮助都很感激。

5fjcxozz

5fjcxozz1#

订单自定义状态键太长,应该更简单和更短,所以我重新访问了您的代码,重命名您的自定义订单状态键(slugs),使其更紧凑:

// 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文件。

wixjitnu

wixjitnu2#

function get_custom_order_statuses(){
        global $wpdb;
        $sotcstatuses  = $wpdb->prefix . 'sot_cstatuses';
        $result=$wpdb->get_results("select * from $sotcstatuses");
        foreach ($result as $row){
    return array(
        'wc-'.$row->statuskey        => __($row->statuslable),     // Bestellung in Bearbeitung
    );
}
}

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

相关问题