Skip to content

Instantly share code, notes, and snippets.

@zorem
Created January 2, 2020 10:39
Show Gist options
  • Save zorem/af58af70122ebaad7190d18faa8e3404 to your computer and use it in GitHub Desktop.
Save zorem/af58af70122ebaad7190d18faa8e3404 to your computer and use it in GitHub Desktop.
Code snippet for create custom order status
<?php
// Add this code to your theme functions.php file or a custom plugin
add_action('init', 'register_order_status');
//add status after completed
add_filter('wc_order_statuses', 'add_custom_status_to_order_statuses');
//Custom Statuses in admin reports
add_filter('woocommerce_reports_order_statuses', 'include_custom_order_status_to_reports', 20, 1);
// for automate woo to check order is paid
add_filter('woocommerce_order_is_paid_statuses', 'custom_status_woocommerce_order_is_paid_statuses');
/*** Register new status : Delivered
* https://www.zorem.com/how-to-create-custom-order-status-in-woocommerce/
**/
function register_order_status()
{
// Replace 'Custom Status' with your order status name
// Replace 'custom-status' with your status slug
register_post_status('wc-custom-status', array(
'label' => __('Custom Status', 'text-domain'),
'public' => true,
'show_in_admin_status_list' => true,
'show_in_admin_all_list' => true,
'exclude_from_search' => false,
'label_count' => _n_noop('Custom Status <span class="count">(%s)</span>', 'Custom Status <span class="count">(%s)</span>', 'text-domain')
));
}
/*
* add status after completed
*/
function add_custom_status_to_order_statuses($order_statuses)
{
// Replace 'Custom Status' with your order status name
// Replace 'custom-status' with your status slug
$new_order_statuses = array();
foreach ($order_statuses as $key => $status) {
$new_order_statuses[$key] = $status;
if ('wc-completed' === $key) {
$new_order_statuses['wc-custom-status'] = __('Custom Status', 'text-domain');
}
}
return $new_order_statuses;
}
/*
* Adding the custom order status to the default woocommerce order statuses
*/
function include_custom_order_status_to_reports($statuses)
{
// Replace 'custom-status' with your status slug
if ($statuses)
$statuses[] = 'custom-status';
return $statuses;
}
/*
* mark status as a paid.
*/
function custom_status_woocommerce_order_is_paid_statuses($statuses)
{
// Replace 'custom-status' with your status slug
$statuses[] = 'custom-status';
return $statuses;
}
@db151
Copy link

db151 commented Sep 30, 2021

hello great code great job.
If I would like to add 2 custom order states?
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment