13

I've tried the below code to create custom status.

add_action( 'init', function() {
    $term = get_term_by( 'name', 'shipped', 'shop_order_status' );
    if ( ! $term ) {
        wp_insert_term( 'shipped', 'shop_order_status' );
    }
} );

But it is not working. I tried some other methods also. Please can anyone help me on this..

2 Answers 2

17

This is what I have used to create a custom order status called "Invoiced". Add this to your theme's functions.php

// New order status AFTER woo 2.2
add_action( 'init', 'register_my_new_order_statuses' );

function register_my_new_order_statuses() {
    register_post_status( 'wc-invoiced', array(
        'label'                     => _x( 'Invoiced', 'Order status', 'woocommerce' ),
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Invoiced <span class="count">(%s)</span>', 'Invoiced<span class="count">(%s)</span>', 'woocommerce' )
    ) );
}

add_filter( 'wc_order_statuses', 'my_new_wc_order_statuses' );

// Register in wc_order_statuses.
function my_new_wc_order_statuses( $order_statuses ) {
    $order_statuses['wc-invoiced'] = _x( 'Invoiced', 'Order status', 'woocommerce' );

    return $order_statuses;
}

In order to add your new status to the admin Bulk-edit dropdown you have to use javascript. Add your function to the admin_footer action. My function then looks something like this:

function custom_bulk_admin_footer() {
            global $post_type;

            if ( $post_type == 'shop_order' ) {
                ?>
                    <script type="text/javascript">
                        jQuery(document).ready(function() {
                            jQuery('<option>').val('mark_invoiced').text('<?php _e( 'Mark invoiced', 'textdomain' ); ?>').appendTo("select[name='action']");
                            jQuery('<option>').val('mark_invoiced').text('<?php _e( 'Mark invoiced', 'textdomain' ); ?>').appendTo("select[name='action2']");   
                        });
                    </script>
                <?php
            }
        }

The action is added two times because there are one bulk action at the top and another at the bottom of the order list.

7
  • @AparnaMathew glad it is working. :)
    – Magnetize
    Aug 24, 2015 at 7:50
  • 1
    @Nathan Hi there! I do actually, but you have to add the bulk actions via js. Edited my answer as it is too long for a comment.
    – Magnetize
    Jun 4, 2018 at 8:03
  • 1
    Does this still apply to WooCommerce 3.x ?
    – Amjad
    Jun 9, 2018 at 21:01
  • 1
    @Amjad indeed it does, I'm using it as we speak. :)
    – Magnetize
    Jun 10, 2018 at 8:50
  • 1
    Indeed it works on 3.x as well. Thanks @Magnetize!
    – Amjad
    Jun 11, 2018 at 22:12
4

As a completion for above and to not use JavaScript to add the actions in bulk actions list, to add/reorder bulk actions you can use the hook bulk_actions-edit-shop_order. For example:

function rename_or_reorder_bulk_actions( $actions ) {
    $actions = array(
        // this is the order in which the actions are displayed; you can switch them
        'mark_processing'      => __( 'Mark placed', 'textdomain' ), // rewrite an existing status
        'mark_invoiced'        => __( 'Mark invoiced', 'textdomain' ), // adding a new one
        'mark_completed'       => $actions['mark_completed'],
        'remove_personal_data' => $actions['remove_personal_data'],
        'trash'                => $actions['trash'],
    );
    return $actions;
}
add_filter( 'bulk_actions-edit-shop_order', 'rename_or_reorder_bulk_actions', 20 );

Cheers!

Not the answer you're looking for? Browse other questions tagged or ask your own question.