在 WooCommerce 管理菜单中包含来自自定义订单状态的订单计数

Include order count from custom order status in WooCommerce admin menu

我在 functions.php

中使用此代码添加了自定义订单状态
// Register new order status
function register_produktionsklar_order_status() {
    register_post_status( 'wc-produktionsklar', array(
        'label'                     => 'Klar til produktion',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Klar til produktion (%s)', 'Klar til produktion (%s)' )
    ) );
}
add_action( 'init', 'register_produktionsklar_order_status' );

// Add new order status to list of WC Order statuses
function add_produktionsklar_to_order_statuses( $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-afventer-godkend' === $key ) {
            $new_order_statuses['wc-produktionsklar'] = 'Klar til produktion';
        }
    }

    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_produktionsklar_to_order_statuses' );

我想将此自定义订单状态包括在订单计数中。当我将状态更改为此自定义状态时,该订单不再计入未结订单数。 (为清楚起见,请参阅附图)

任何帮助都会很棒

默认情况下,仅统计状态为 'processing' 的订单并将其添加到订单计数中。

要对此进行调整,我们将使用 woocommerce_menu_order_count 过滤器挂钩和 wc_orders_count() 函数,其中 return 特定订单状态的订单计数。

所以你得到:

/**
 * Add orders count of a specific order status
 */
function filter_woocommerce_menu_order_count( $wc_processing_order_count ) {
    // Call function and pass custom order status
    $order_count_produktionsklar = wc_orders_count( 'produktionsklar' );
    
    return $wc_processing_order_count + $order_count_produktionsklar;
}
add_filter( 'woocommerce_menu_order_count', 'filter_woocommerce_menu_order_count', 10, 1 );

我也用这个更新的代码重写了你的代码。例如,init 挂钩已替换为 woocommerce_register_shop_order_post_statuses 以注册自定义订单状态。

/**
 * Register order status
 */
function filter_woocommerce_register_shop_order_post_statuses( $order_statuses ) {
    // Status must start with "wc-"
    $order_statuses['wc-produktionsklar'] = array(
        'label'                     => _x( 'Klar til produktion', 'Order status', 'woocommerce' ),
        'public'                    => false,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        /* translators: %s: number of orders */
        'label_count'               => _n_noop( 'Klar til produktion <span class="count">(%s)</span>', 'Klar til produktion <span class="count">(%s)</span>', 'woocommerce' ),       
    );
    
    return $order_statuses;
}
add_filter( 'woocommerce_register_shop_order_post_statuses', 'filter_woocommerce_register_shop_order_post_statuses', 10, 1 );

/**
 * Show order status in the dropdown @ single order
 */
function filter_wc_order_statuses( $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 ) {
            // Status must start with "wc-"
            $new_order_statuses['wc-produktionsklar'] = _x( 'Klar til produktion', 'Order status', 'woocommerce' );
        }
    }

    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'filter_wc_order_statuses', 10, 1 );

/**
 * Show order status in the dropdown @ bulk actions
 */
function filter_bulk_actions_edit_shop_order( $bulk_actions ) {
    // Note: "mark_" must be there instead of "wc"
    $bulk_actions['mark_produktionsklar'] = __( 'Klar til produktion', 'woocommerce' );
    return $bulk_actions;
}
add_filter( 'bulk_actions-edit-shop_order', 'filter_bulk_actions_edit_shop_order', 10, 1 );