在 WooCommerce 管理菜单中包含来自自定义订单状态的订单数 [英] Include order count from custom order status in WooCommerce admin menu

查看:40
本文介绍了在 WooCommerce 管理菜单中包含来自自定义订单状态的订单数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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' );


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


I would like to include this custom order status in the order count. When I change the status to this custom status, the order is no longer counted in the open orders number. (see attached image for clarity)

任何帮助都会很棒

推荐答案

默认情况下,只有状态为正在处理"的订单才会被统计并添加到订单计数中.

By default only orders with the status 'processing' are counted and added to the orders count number.

为了调整这一点,我们将使用 woocommerce_menu_order_count 过滤器钩子和 wc_orders_count() 函数,它们返回特定订单状态的订单数.

To adjust this we are going to use the woocommerce_menu_order_count filter hook and wc_orders_count() function, which return the orders count of a specific order status.

所以你得到:

/**
 * 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 以注册自定义订单状态.


I also have rewritten your code with this updated code. For example, the init hook has been replaced with woocommerce_register_shop_order_post_statuses to register custom order 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 );

这篇关于在 WooCommerce 管理菜单中包含来自自定义订单状态的订单数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆