WooCommerce已經(jīng)允許批量更改默認(rèn)訂單狀態(tài)。但是,如果您注冊了自己的自定義訂單狀態(tài),現(xiàn)在又想將其添加到“訂單”頁面上的批量操作中,該怎么辦。

您可能找到一個教程,是通過jQuery添加了此下拉列表選擇的新選項,但是這種教程已經(jīng)過時了,因為從WordPress 3.5.0開始,您可以使用 bulk_actions-{screen_id} 鉤子來完成。
<?php
/*
* 在下拉菜單中添加您的自定義批量操作
* @since 3.5.0
*/
add_filter( 'bulk_actions-edit-shop_order', 'misha_register_bulk_action' ); // edit-shop_order是訂單頁面的屏幕ID
function misha_register_bulk_action( $bulk_actions ) {
$bulk_actions['mark_awaiting_shipment'] = 'Mark awaiting shipment'; // <option value="mark_awaiting_shipment">Mark awaiting shipment</option>
return $bulk_actions;
}
/*
* 批量動作處理程序
* 確保掛鉤中的“動作名稱”與上述函數(shù)中的選項值相同
*/
add_action( 'admin_action_mark_awaiting_shipment', 'misha_bulk_process_custom_status' ); // admin_action_{動作名稱}
function misha_bulk_process_custom_status() {
// //如果未顯示具有訂單ID的數(shù)組,則在
if( !isset( $_REQUEST['post'] ) && !is_array( $_REQUEST['post'] ) )
return;
foreach( $_REQUEST['post'] as $order_id ) {
$order = new WC_Order( $order_id );
$order_note = 'That\'s what happened by bulk edit:';
$order->update_status( 'misha-shipment', $order_note, true ); // "misha-shipment" 是訂單狀態(tài)名稱(請勿使用wc-misha-shipment)
}
// 當(dāng)然不需要使用add_query_arg(),您可以內(nèi)聯(lián)構(gòu)建URL
$location = add_query_arg( array(
'post_type' => 'shop_order',
'marked_awaiting_shipment' => 1, // 標(biāo)識 ED_awaiting_shipment=1 只是為了給通知設(shè)置 $_GET 參數(shù)
'changed' => count( $_REQUEST['post'] ), // //更改后的訂單數(shù)
'ids' => join( $_REQUEST['post'], ',' ),
'post_status' => 'all'
), 'edit.php' );
wp_redirect( admin_url( $location ) );
exit;
}
/*
* 通知
*/
add_action('admin_notices', 'misha_custom_order_status_notices');
function misha_custom_order_status_notices() {
global $pagenow, $typenow;
if( $typenow == 'shop_order'
&& $pagenow == 'edit.php'
&& isset( $_REQUEST['marked_awaiting_shipment'] )
&& $_REQUEST['marked_awaiting_shipment'] == 1
&& isset( $_REQUEST['changed'] ) ) {
$message = sprintf( _n( 'Order status changed.', '%s order statuses changed.', $_REQUEST['changed'] ), number_format_i18n( $_REQUEST['changed'] ) );
echo "<div class=\"updated\"><p>{$message}</p></div>";
}
}
實際上,必須在線上進(jìn)行的替換:(10批量動作標(biāo)簽和值),19(掛鉤中的動作名稱)和31(您的自定義訂單狀態(tài)段)。其他更改是可選的。
該代碼可以在functions.php或插件文件中使用。
第 30 行中的訂購備注(您可以在編輯訂單頁面上找到):

處理批量訂單狀態(tài)更改后的通知:

如果您對 WooCommerce 的使用還不太了解,或者想系統(tǒng)學(xué)習(xí) WooCommerce 開發(fā),可以學(xué)習(xí)教程《WooCommerce 開發(fā)指南視頻課程(使用詳解/主題開發(fā)/支付寶網(wǎng)關(guān)開發(fā))》。
聲明:原文出自 https://rudrastyh.com/woocommerce/bulk-change-custom-order-status.html ,由WordPress大學(xué)翻譯整理,轉(zhuǎn)載請保留本聲明。





校長你好,我又來了,之前看了你的添加自定義訂單狀態(tài)后很有啟發(fā),現(xiàn)在我想給我的自定義狀態(tài)加一些樣式,請問我要怎么做呢?