Last active
November 24, 2021 09:52
-
-
Save blogjunkie/cb5ff5f97023a58088cf4f630543e285 to your computer and use it in GitHub Desktop.
Add 'Awaiting pre-order' custom status to WooCommerce with bulk edit and custom status color
This file contains hidden or 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 // Don't copy this line | |
/** | |
* Add 'Awaiting pre-order' custom status | |
* | |
* 1. Register new order status | |
* 2. Add to list of WC order statuses | |
* 3. Add your custom bulk action in dropdown | |
* 4. Bulk action handler | |
* 5. Admin notices for bulk action | |
* 6. Style the status in the orders list | |
* | |
* CREDITS | |
* https://jilt.com/blog/woocommerce-custom-order-status-2/ | |
* https://rudrastyh.com/woocommerce/bulk-change-custom-order-status.html | |
* https://stackoverflow.com/a/49341814 | |
*/ | |
/* | |
* 1. Register new order status | |
*/ | |
function aw_register_awaiting_preorder_order_status() { | |
register_post_status( 'wc-awaiting-preorder', array( | |
'label' => 'Awaiting pre-order', | |
'public' => true, | |
'exclude_from_search' => false, | |
'show_in_admin_all_list' => true, | |
'show_in_admin_status_list' => true, | |
'label_count' => _n_noop( 'Awaiting pre-order (%s)', 'Awaiting pre-order (%s)' ) | |
) ); | |
} | |
add_action( 'init', 'aw_register_awaiting_preorder_order_status' ); | |
/* | |
* 2. Add to list of WC order statuses | |
*/ | |
function aw_add_awaiting_preorder_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-pending' === $key ) { | |
$new_order_statuses['wc-awaiting-preorder'] = 'Awaiting pre-order'; | |
} | |
} | |
return $new_order_statuses; | |
} | |
add_filter( 'wc_order_statuses', 'aw_add_awaiting_preorder_to_order_statuses' ); | |
/* | |
* 3. Add your custom bulk action in dropdown | |
*/ | |
add_filter( 'bulk_actions-edit-shop_order', 'aw_register_bulk_action' ); // edit-shop_order is the screen ID of the orders page | |
function aw_register_bulk_action( $bulk_actions ) { | |
$bulk_actions['mark_awaiting_preorder'] = 'Mark awaiting pre-order'; // <option value="mark_awaiting_preorder">Mark awaiting pre-order</option> | |
return $bulk_actions; | |
} | |
/* | |
* 4. Bulk action handler | |
* Make sure that "action name" in the hook is the same like the option value from the above function | |
*/ | |
add_action( 'admin_action_mark_awaiting_preorder', 'aw_bulk_process_custom_status' ); // admin_action_{action name} | |
function aw_bulk_process_custom_status() { | |
// if an array with order IDs is not presented, exit the function | |
if( !isset( $_REQUEST['post'] ) && !is_array( $_REQUEST['post'] ) ) | |
return; | |
foreach( $_REQUEST['post'] as $order_id ) { | |
$order = new WC_Order( $order_id ); | |
$order_note = 'Order updated by admin:'; | |
$order->update_status( 'wc-awaiting-preorder', $order_note, true ); | |
} | |
// of course using add_query_arg() is not required, you can build your URL inline | |
$location = add_query_arg( array( | |
'post_type' => 'shop_order', | |
'marked_awaiting_preorder' => 1, // marked_awaiting_preorder=1 is just the $_GET variable for notices | |
'changed' => count( $_REQUEST['post'] ), // number of changed orders | |
'ids' => join( $_REQUEST['post'], ',' ), | |
'post_status' => 'all' | |
), 'edit.php' ); | |
wp_redirect( admin_url( $location ) ); | |
exit; | |
} | |
/* | |
* 5. Admin notices for bulk action | |
*/ | |
add_action('admin_notices', 'aw_custom_order_status_notices'); | |
function aw_custom_order_status_notices() { | |
global $pagenow, $typenow; | |
if( $typenow == 'shop_order' | |
&& $pagenow == 'edit.php' | |
&& isset( $_REQUEST['marked_awaiting_preorder'] ) | |
&& $_REQUEST['marked_awaiting_preorder'] == 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>"; | |
} | |
} | |
/* | |
* 6. Style the status in the orders list | |
*/ | |
add_action('admin_head', 'aw_styling_admin_order_list' ); | |
function aw_styling_admin_order_list() { | |
global $pagenow, $post; | |
if( $pagenow != 'edit.php') return; // Exit | |
if( get_post_type($post->ID) != 'shop_order' ) return; // Exit | |
// HERE we set your custom status | |
$order_status = 'Dispatched'; // <==== HERE | |
?> | |
<style> | |
.order-status.status-awaiting-preorder { | |
background: #334862; | |
color: #fff; | |
} | |
</style> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment