向具有自定义状态的 WooCommerce 订单显示 "complete" 操作按钮

Show "complete" action button to WooCommerce order with a custom status

我使用以下代码添加了自定义 WooCommerce 状态并想要

function register_shipped_status() {

    register_post_status( 'wc-shipped', array(
        'label'                     => 'Shipped',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Versendet <span class="count">(%s)</span>', 'Versendet <span class="count">(%s)</span>' )
    ) );

}
add_action( 'init', 'register_shipped_status' );

function add_shipped_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-processing' === $key ) {
            $new_order_statuses['wc-shipped'] = 'Versendet';
        }
    }

    return $new_order_statuses;

}
add_filter( 'wc_order_statuses', 'add_shipped_to_order_statuses' );

这工作正常。 WooCommerce 状态是可选的,但我想要订单列表中的操作按钮 "completed"。

查看截图:

有什么方法可以将此 WooCommerce 按钮添加到具有我的自定义 WooCommerce 状态的所有订单吗?

要为具有自定义订单状态的订单设置 "complete" 操作按钮,请使用以下内容:

add_filter( 'woocommerce_admin_order_actions', 'customize_admin_order_actions', 10, 2 );
function customize_admin_order_actions( $actions, $order ) {
    // Display the "complete" action button for orders that have a 'shipped' status
    if ( $order->has_status('shipped') ) {
        $actions['complete'] = array(
            'url'    => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=completed&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ),
            'name'   => __( 'Complete', 'woocommerce' ),
            'action' => 'complete',
        );
    }
    return $actions;
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

相关: