Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@claudiosanches
Created September 12, 2014 13:49
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save claudiosanches/e68e891d593c4c989d9d to your computer and use it in GitHub Desktop.
Save claudiosanches/e68e891d593c4c989d9d to your computer and use it in GitHub Desktop.
WooCommerce 2.2 - Register new order statuses.
<?php
// My new order statuses.
function register_my_new_order_statuses() {
register_post_status( 'wc-status-name', array(
'label' => _x( 'Status Name', 'Order status', 'textdomain' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Status name <span class="count">(%s)</span>', 'Status names <span class="count">(%s)</span>', 'textdomain' )
) );
}
add_action( 'init', 'register_my_new_order_statuses' );
// Register in wc_order_statuses.
function my_new_wc_order_statuses( $order_statuses ) {
$order_statuses['wc-status-name'] = _x( 'Status Name', 'Order status', 'textdomain' );
return $order_statuses;
}
add_filter( 'wc_order_statuses', 'my_new_wc_order_statuses' );
@wicked4u2c
Copy link

I want to add several statuses, I've tried different ways but it only adds one. What do I need to do to add multiple statuses? Thanks in advance.

@Giancarlo67
Copy link

As this example uses the naming "statuses", there might be a possibility to add multiple statuses.
But as I haven't figured it out myself, I just copied this piece of code for each additional status.

@seuo
Copy link

seuo commented Nov 17, 2014

Hey, thanks for the snippet. I've successfully added my custom status "Quote". I'm just having trouble having it set to a pending or onhold wc status so i can edit it at anytime. At the moment it's down as paid by default and i can't seem to edit the order unless i refund first.
Any ideas?

@DenUil
Copy link

DenUil commented Nov 24, 2014

I tried to adopt it a bit to make it use multiple statuses given by the admin settings.

    private function __construct() {
        add_action('init', array($this, 'register_additional_order_statuses'),10);
        add_filter('wc_order_statuses', array($this, 'add_additional_statuses_to_order_statuses'));
    }

    public function register_additional_order_statuses() {
        $additional_statuses_raw = trim(get_option('additional_statuses'));
        $add_statuses = explode("\n", $additional_statuses_raw);
        $add_statuses = array_filter($add_statuses, 'trim');
        foreach ($add_statuses as $add_status) {
            $wcName = "wc-" . implode("-", explode(" ", $add_status));
            register_post_status($wcName, array(
                'label' => _x($add_status, 'Order status', 'textdomain'),
                'public' => true,
                'exclude_from_search' => false,
                'show_in_admin_all_list' => true,
                'show_in_admin_status_list' => true,
                'label_count' => _n_noop($add_status . '<span class="count">(%s)</span>', $add_status . ' <span class="count">(%s)</span>', 'woocommerce' )
            ));
        }
    }

    public function add_additional_statuses_to_order_statuses($order_statuses) {
        $additional_statuses_raw = trim(get_option('additional_statuses'));
        $add_statuses = explode("\n", $additional_statuses_raw);
        $add_statuses = array_filter($add_statuses, 'trim');
        foreach ($add_statuses as $add_status) {
            $wcName = "wc-" . implode("-", explode(" ", $add_status));
            $order_statuses[$wcName] = _x($add_status, 'Order status', 'textdomain');
        }
        return $order_statuses;
    }

However I am struggling with the fact that now, when I look at my order field, none orders are displayed.
The "All" tab displays that there are 4, but for some reason they aren't displayed.
I put a screenshot here:
https://www.dropbox.com/s/6te775tmij5oy6p/screenshot.jpg?dl=0

Any thoughts?

@leozamprogno
Copy link

I just want to change the name of the existing orders. Didn't find how to do that anywhere.
I used this code and it changed the names in the main list, but in the actions dropdown and the filter in orders list dons't change.

Any thoughts? Thanks very much!

function wc_get_order_statuses() {
$order_statuses = array(
'wc-pending' => _x( '1 - Em orçamento', 'Order status', 'woocommerce' ),
'wc-processing' => _x( '2 - Pedido em atendimento', 'Order status', 'woocommerce' ),
'wc-on-hold' => _x( '3 - Pedido enviado', 'Order status', 'woocommerce' ),
'wc-completed' => _x( '4 - Pedido realizado', 'Order status', 'woocommerce' ),
'wc-cancelled' => _x( 'Cancelado', 'Order status', 'woocommerce' ),
'wc-refunded' => _x( 'Reembolso ao cliente', 'Order status', 'woocommerce' ),
'wc-failed' => _x( 'Falhou', 'Order status', 'woocommerce' ),
);
return apply_filters( 'wc_order_statuses', $order_statuses );
}

@jwad93
Copy link

jwad93 commented Jan 11, 2016

How can I also send the email with the new order status ? Just like the woocommerce sends an email to the customer when change the order status to "Completed".

@rajat24
Copy link

rajat24 commented Feb 16, 2016

I also have a similar issue like @jwad93. I want to send emails as soon as the status of product gets changed.
But in Woocommerce>Settings>Email only predefined options are present. For every status we hava a file separately stored in woocommerce>templates>emails.

So do I need to create a new file for every custom order status??

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