Skip to content

Instantly share code, notes, and snippets.

@zorem
Last active November 9, 2024 00:45
Show Gist options
  • Save zorem/f9a1d2fdb03ff70c0413625b5f689db0 to your computer and use it in GitHub Desktop.
Save zorem/f9a1d2fdb03ff70c0413625b5f689db0 to your computer and use it in GitHub Desktop.
Code snippet for creating custom order status
<?php
/*
* This function is registering our custom status as a post status in WordPress.
*/
add_action('init', 'register_custom_order_status');
function register_custom_order_status()
{
register_post_status('wc-my-custom-status', array( // Replace 'my-custom-status' with your custom order status slug
'label' => __('My Custom Status', 'text-domain'), // Replace 'My Custom Status' with your custom order status name
'public' => true,
'show_in_admin_status_list' => true,
'show_in_admin_all_list' => true,
'exclude_from_search' => false,
'label_count' => _n_noop('My Custom Status <span class="count">(%s)</span>', 'My Custom Status <span class="count">(%s)</span>', 'text-domain') // Replace 'My Custom Status' with your custom order status name
));
}
/*
* The function is going to add this new custom post status into the list of available order statuses within the WooCommerce “Orders” and “Edit Orders” pages so that we can actually use it. We want to pass in the current order statuses so that we can go through them and insert our order status into the list where we’d like it.
*/
add_filter('wc_order_statuses', 'add_custom_status_to_order_statuses');
function add_custom_status_to_order_statuses($order_statuses)
{
// Replace 'my-custom-status' with your custom order status slug
// Replace 'My Custom Status' with your custom order status name
$new_order_statuses = array();
foreach ($order_statuses as $key => $status) {
$new_order_statuses[$key] = $status;
if ('wc-completed' === $key) {
$new_order_statuses['wc-my-custom-status'] = __('My Custom Status', 'text-domain'); x
}
}
return $new_order_statuses;
}
/*
* The function is going to add this new custom order status to Orders reports.
*/
add_filter('woocommerce_reports_order_statuses', 'include_custom_order_status_to_reports', 20, 1);
function include_custom_order_status_to_reports($statuses)
{
// Replace 'my-custom-status' with your custom order status slug
if ($statuses)
$statuses[] = 'my-custom-status';
return $statuses;
}
/*
* The function is going to add this new custom order status as paid statuses.
*/
add_filter('woocommerce_order_is_paid_statuses', 'custom_status_woocommerce_order_is_paid_statuses');
function custom_status_woocommerce_order_is_paid_statuses($statuses)
{
$statuses[] = 'my-custom-status';
return $statuses;
}
/*
* The function is going to add your custom order status in bulk action dropdown in orders page
*/
add_filter( 'bulk_actions-edit-shop_order', 'add_bulk_actions_change_order_status' , 50, 1 );
function add_bulk_actions_change_order_status($bulk_actions){
// Replace 'my-custom-status' with your custom order status slug
// Replace 'My Custom Status' with your custom order status name
$bulk_actions['mark_my-custom-status'] = __( 'Change status to My Custom Status', 'woocommerce' );
return $bulk_actions;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment