WooCommerce 內建有幾種預設的訂單狀態,但這些狀態不見得適合每個商店,因為店家通常都會有一些自己的內部流程,所以為了方便我們可以新增客製的訂單狀態,讓店家的內部流程可以更順利一點。
首先透過 register_post_status 這個函式來新增一個客製化訂單,另外透過 wc_order_statuses 這個 filter, 來在訂單編輯頁面中加入新的訂單狀態
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function register_awaiting_shipment_order_status() { | |
register_post_status( 'wc-awaiting-shipment', array( | |
'label' => '等待運送', | |
'public' => true, | |
'exclude_from_search' => false, | |
'show_in_admin_all_list' => true, | |
'show_in_admin_status_list' => true, | |
'label_count' => _n_noop( '等待運送 <span class="count">(%s)</span>', '等待運送 <span class="count">(%s)</span>' ) | |
) ); | |
} | |
add_action( 'init', 'register_awaiting_shipment_order_status' ); | |
// Add to list of WC Order statuses | |
function add_awaiting_shipment_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-awaiting-shipment'] = '等待運送'; | |
} | |
} | |
return $new_order_statuses; | |
} | |
add_filter( 'wc_order_statuses', 'add_awaiting_shipment_to_order_statuses' ); |
在訂單編輯頁面的訂單狀態中,就可以看到新增的狀態。
但是如果要編輯訂單狀態必須一筆一筆訂單改,那也太沒有效率了,所以我們要在訂單清單中的批次處理動作中,新增我們增加的訂單狀態。
首先透過 handle_bulk_actions-edit-shop_order, 來更新選擇的訂單狀態。
另外透過 admin_notices, 來顯示你要提醒使用者的相關訊息。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//處理訂單狀態 | |
add_filter( 'handle_bulk_actions-edit-shop_order', 'wootest_handle_bulk_actions_edit_product', 10, 3 ); | |
function wootest_handle_bulk_actions_edit_product( $redirect_to, $action, $post_ids ) { | |
global $typenow; | |
$post_type = $typenow; | |
if( $post_type == 'shop_order' ) { | |
$wp_list_table = _get_list_table('WP_Posts_List_Table'); | |
$action = $wp_list_table->current_action(); | |
$allowed_actions = array( 'wc-awaiting-shipment' ); | |
if( !in_array( $action, $allowed_actions ) ) return; | |
switch( $action ) { | |
case "wc-awaiting-shipment": | |
if ( count($post_ids) > 0 ) { | |
foreach( $post_ids as $order_id) { | |
$order = new WC_Order($order_id); | |
if($order->status != 'wc-awaiting-shipment' ) { | |
$order->update_status('wc-awaiting-shipment', 'order_note'); | |
} | |
} | |
} | |
$redirect_to = add_query_arg( 'wootest_shop_order_update_results', count( $post_ids ), $redirect_to ); | |
return $redirect_to; | |
break; | |
default: return; | |
} | |
}//end shop_order | |
} | |
//顯示notice | |
add_action( 'admin_notices', 'wootest_bulk_action_edit_shop_order_admin_notice' ); | |
function wootest_bulk_action_edit_shop_order_admin_notice() { | |
if ( isset( $_REQUEST['wootest_shop_order_update_results'] ) ) { | |
$updated_shop_orders_count = intval( $_REQUEST['wootest_shop_order_update_results'] ); | |
echo '<div id="message" class="' . ( $updated_shop_orders_count > 0 ? 'updated' : 'error' ) . '">'; | |
if ( $updated_shop_orders_count > 0 ) { | |
echo '<p>' . __( 'Updated Orders for '. $updated_shop_orders_count .' '. _n( 'order', 'orders', $updated_shop_orders_count, 'wordpress' ).'!', 'wordpress' ) . '</p>'; | |
} else { | |
echo '<p>' . __( 'No Orders were updated!', 'wordpress' ) . '</p>'; | |
} | |
echo '</div>'; | |
} | |
} |
如此商家就可以在訂單清單中批次更新訂單狀態了。
參考文章:
https://rudrastyh.com/woocommerce/bulk-change-custom-order-status.html