【问题标题】:Include order count from custom order status in WooCommerce admin menu在 WooCommerce 管理菜单中包含来自自定义订单状态的订单计数
【发布时间】:2021-09-04 12:03:45
【问题描述】:

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

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

任何帮助都会很棒

【问题讨论】:

    标签: php wordpress woocommerce backend orders


    【解决方案1】:

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

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

    所以你得到:

    /**
     * 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 );
    

    【讨论】:

      猜你喜欢
      • 2020-05-03
      • 2018-09-10
      • 1970-01-01
      • 2021-02-21
      • 1970-01-01
      • 2019-08-27
      • 2018-12-30
      • 2018-08-26
      相关资源
      最近更新 更多