在Woocommerce中重命名多个订单状态

问题描述 投票:3回答:1

我正在尝试通过编辑我的主题的functions.php文件来重命名多个WooCommerce订单状态。我发现几年前发布的一些代码可以改变单个订单状态,但由于我对php非常缺乏经验,我不知道如何扩展它以改变多个状态。理想情况下,我还想将'wc-processing'重命名为'付费',将'wc-on-hold'重命名为'待定'。

这是我发现编辑单个订单状态的代码:

function wc_renaming_order_status( $order_statuses ) {
    foreach ( $order_statuses as $key => $status ) {
        $new_order_statuses[ $key ] = $status;
        if ( 'wc-completed' === $key ) {
            $order_statuses['wc-completed'] = _x( 'Order Received', 'Order status', 'woocommerce' );
        }
    }
    return $order_statuses;
}
add_filter( 'wc_order_statuses', 'wc_renaming_order_status' );

任何人都知道我需要做些什么改变来改变其他状态?

php wordpress woocommerce hook-woocommerce orders
1个回答
8
投票

由于存在待处理订单状态,您还需要重命名现有的“待处理”状态。如果不是,您将获得具有相同“待定”标签的2种不同状态。

首先重命名这些订单状态:

add_filter( 'wc_order_statuses', 'rename_order_statuses', 20, 1 );
function rename_order_statuses( $order_statuses ) {
    $order_statuses['wc-completed']  = _x( 'Order Received', 'Order status', 'woocommerce' );
    $order_statuses['wc-processing'] = _x( 'Paid', 'Order status', 'woocommerce' );
    $order_statuses['wc-on-hold']    = _x( 'Pending', 'Order status', 'woocommerce' );
    $order_statuses['wc-pending']    = _x( 'Waiting', 'Order status', 'woocommerce' );

    return $order_statuses;
}

此外,在批量修改订单列表下拉列表中:

add_filter( 'bulk_actions-edit-shop_order', 'custom_dropdown_bulk_actions_shop_order', 20, 1 );
function custom_dropdown_bulk_actions_shop_order( $actions ) {
    $actions['mark_processing'] = __( 'Mark paid', 'woocommerce' );
    $actions['mark_on-hold']    = __( 'Mark pending', 'woocommerce' );
    $actions['mark_completed']  = __( 'Mark order received', 'woocommerce' );

    return $actions;
}

enter image description here

而这也需要(对于顶级菜单):

foreach( array( 'post', 'shop_order' ) as $hook )
    add_filter( "views_edit-$hook", 'shop_order_modified_views' );

function shop_order_modified_views( $views ){
    if( isset( $views['wc-completed'] ) )
        $views['wc-completed'] = str_replace( 'Completed', __( 'Order Received', 'woocommerce'), $views['wc-completed'] );

    if( isset( $views['wc-processing'] ) )
        $views['wc-processing'] = str_replace( 'Processing', __( 'Paid', 'woocommerce'), $views['wc-processing'] );

    if( isset( $views['wc-on-hold'] ) )
        $views['wc-on-hold'] = str_replace( 'On hold', __( 'Pending', 'woocommerce'), $views['wc-on-hold'] );

    if( isset( $views['wc-pending'] ) )
        $views['wc-pending'] = str_replace( 'Pending', __( 'Stucked', 'woocommerce'), $views['wc-pending'] );

    return $views;
}

(感谢brasofiloChange WP admin post status filter for custom post type

enter image description here

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

自Woocommerce 3.3处理管理员订单列表中的预览弹出窗口(眼睛符号):

Replace order status names everywhere incl. Woocommerce admin order preview

© www.soinside.com 2019 - 2024. All rights reserved.