如何添加 woocommerce 自定义订单状态?

How to add woocommerce custom order status?

我已经使用以下功能向 woocommerce 添加了新的自定义订单状态。

// Register New Order Statuses
function wpex_wc_register_post_statuses() {
 register_post_status( 'wc-custom-order-status', array(
  'label'      => _x( 'Shipping In Progress', 'WooCommerce Order status', 'text_domain' ),
  'public'     => true,
  'exclude_from_search'  => false,
  'show_in_admin_all_list' => true,
  'show_in_admin_status_list' => true,
  'label_count'    => _n_noop( 'Approved (%s)', 'Approved (%s)', 'text_domain' )
 ) );
}
add_filter( 'init', 'wpex_wc_register_post_statuses' );

// Add New Order Statuses to WooCommerce
function wpex_wc_add_order_statuses( $order_statuses ) {
 $order_statuses['wc-custom-order-status'] = _x( 'Shipping In Progress', 'WooCommerce Order status', 'text_domain' );
 return $order_statuses;
}
add_filter( 'wc_order_statuses', 'wpex_wc_add_order_statuses' );

每当我去编辑订单并将订单状态更改为新添加的自定义订单状态并单击“保存订单”按钮时。载入后订单状态自动变为待定订单不在新添加的自定义订单中...

如何克服这个问题...?

您正在注册的订单状态 wc-custom-order-status 太长 - 22 个字符。这导致仅保存 post 状态的前 20 个字符,这使其无效并导致您的问题。

订单状态被注册为post个状态,post个状态有20个字符的限制。

我建议您将您的 wc-custom-order-status 身份名称更新为 wc-shipping-progress,长度刚好是 20 个字符。

我也在post下载你代码的更新版本,仅供参考(我只更改了状态名称):

// Register New Order Statuses
function wpex_wc_register_post_statuses() {
    register_post_status( 'wc-shipping-progress', array(
        'label'                     => _x( 'Shipping In Progress', 'WooCommerce Order status', 'text_domain' ),
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Approved (%s)', 'Approved (%s)', 'text_domain' )
    ) );
}
add_filter( 'init', 'wpex_wc_register_post_statuses' );

// Add New Order Statuses to WooCommerce
function wpex_wc_add_order_statuses( $order_statuses ) {
    $order_statuses['wc-shipping-progress'] = _x( 'Shipping In Progress', 'WooCommerce Order status', 'text_domain' );
    return $order_statuses;
}
add_filter( 'wc_order_statuses', 'wpex_wc_add_order_statuses' );