显示具有自定义状态的WooCommerce订单的“完成”操作按钮

时间:2019-05-15 17:00:50

标签: php wordpress woocommerce hook-woocommerce orders

我使用以下代码添加了自定义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状态是可选的,但是我希望订单列表中的操作按钮“完成”。

查看屏幕截图:

enter image description here

是否可以将WooCommerce按钮添加到所有具有我的自定义WooCommerce状态的订单中?

1 个答案:

答案 0 :(得分:1)

要对具有自定义订单状态的订单使用“完成”操作按钮,请使用以下命令:

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文件中。经过测试,可以正常工作。

enter image description here

相关:Add a custom order status "Shipped" in Woocommerce

相关问题