WooCommerce - 自定义管理员订单状态无效

时间:2017-04-21 16:08:21

标签: php wordpress woocommerce

我使用以下代码在WooCommerce中创建了自定义订单状态:

function register_packed_order_status() {
    register_post_status( 'wc-goods-packed', array(
        'label'                     => 'Gepackt',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Gepackt <span class="count">(%s)</span>', 'Gepackt <span class="count">(%s)</span>' )
    ) );
}
add_action( 'init', 'register_packed_order_status' );

之后,我在下拉列表中按正确的顺序输入状态:

// Add to list of WC Order statuses
function add_packed_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-on-hold' === $key ) {
            $new_order_statuses['wc-goods-packed'] = 'Gepackt';
        }
    }

    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_packed_to_order_statuses' );

可行 - 我可以在产品编辑屏幕中选择订单状态并保存订单。为了防止商店经理始终需要编辑订单明细屏幕,我想在订单概览屏幕的订单操作列中添加一个按钮。我使用以下代码执行此操作:

add_filter( 'woocommerce_admin_order_actions', 'add_cancel_order_actions_button', PHP_INT_MAX, 2 );
function add_cancel_order_actions_button( $actions, $the_order ) {
    if ( ! $the_order->has_status( array( 'wc-goods-packed' ) ) ) {
        $actions['wc-goods-packed'] = array(
            'url'       => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=wc-goods-packed&order_id=' . $the_order->id ), 'woocommerce-mark-order-status' ),
            'name'      => __( 'Gepackt', 'woocommerce' ),
            'action'    => "view packed", // setting "view" for proper button CSS
        );
    }
    return $actions;
}

我可以在概览中看到按钮(http://d.pr/i/ZC1t),我可以点击它,但状态不会改变。你知道为什么会这样吗?我看不出我的代码有什么问题。

谢谢!

1 个答案:

答案 0 :(得分:1)

我明白了。我需要设置的状态不是'wc-goods-packed',而只是'货物包装',因为WC剥离了“wc - ”。

相关问题