Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

php - How to add woocommerce custom order status?

I have added new custom order status to woocommerce by using following function.

// 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' );
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The order status that you're registering wc-custom-order-status is too long - 22 characters. This results in saving only the first 20 characters of the post status, which makes it invalid and causes your issue.

The order statuses are registered as post statuses, and post statuses have a limitation of 20 characters.

I suggest that you update your wc-custom-order-status status name to wc-shipping-progress, which is exactly 20 characters in length.

I'm also posting the updated version of your code, just for reference (I've only changed the status name):

// 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' );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...