Last updated on October 18, 2023

WooCommerce custom order status

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Manage orders with custom status settings in WooCommerce.

A custom order status in WooCommerce can be useful when you want to add a new status to track the progress of an order or to indicate a specific condition. For example, you might want to create a “Backordered” status to indicate that an item is temporarily out of stock.

To create a custom order status in WooCommerce, you can use the register_post_status() function provided by WordPress. Here’s an example code snippet that demonstrates how to create a custom order status called “Backordered”:

/**
 * Register custom order status: Backordered
 */
function wpsnippets_register_backordered_order_status() {
    register_post_status( 'wc-backordered', array(
        'label'                     => _x( 'Backordered', 'Order status', 'text-domain' ),
        'public'                    => false,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Backordered <span class="count">(%s)</span>', 'Backordered <span class="count">(%s)</span>', 'text-domain' )
    ) );
}
add_action( 'init', 'wpsnippets_register_backordered_order_status' );

In this code snippet, we define a new custom order status called “Backordered” using the register_post_status() function. The register_post_status() function accepts two parameters: the status name and an array of arguments.

The arguments array defines various properties of the custom order status, such as the label, visibility, and count label. You can customize these properties according to your requirements.

Once you’ve added this code to your theme’s functions.php file or a custom plugin, the “Backordered” status will be available in the WooCommerce order status dropdown and can be assigned to orders.

Examples

Example 1: Creating a Custom Order Status in WooCommerce

This example demonstrates how to create a custom order status in WooCommerce using the register_post_status() function.

function wpsnippets_add_custom_order_status() {
    register_post_status( 'wc-custom-status', array(
        'label'                     => _x( 'Custom Status', '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( 'Custom Status <span class="count">(%s)</span>', 'Custom Status <span class="count">(%s)</span>', 'woocommerce' )
    ) );
}
add_action( 'init', 'wpsnippets_add_custom_order_status' );

This code registers a new custom order status called “Custom Status” in WooCommerce. The status will be visible in the order list and order details pages in the admin area. The register_post_status() function is used to define the status properties, such as label, visibility, and count display.

Example 2: Assigning a Custom Order Status to an Order

This example demonstrates how to assign a custom order status to an order in WooCommerce using the wc_update_order() function.

function wpsnippets_assign_custom_order_status( $order_id ) {
    $order = wc_get_order( $order_id );
    $order->update_status( 'wc-custom-status' );
}
add_action( 'woocommerce_thankyou', 'wpsnippets_assign_custom_order_status' );

This code assigns the custom order status “Custom Status” to an order when the customer completes the checkout process. The wc_update_order() function is used to update the order status to the custom status. The woocommerce_thankyou action hook is used to trigger the status update after the order is placed.

Example 3: Displaying Custom Order Status in Order Details

This example demonstrates how to display the custom order status in the order details page in WooCommerce.

function wpsnippets_display_custom_order_status() {
    $order_id = get_the_ID();
    $order = wc_get_order( $order_id );
    $status = $order->get_status();

    if ( 'wc-custom-status' === $status ) {
        echo '<p class="custom-status">Custom Status</p>';
    }
}
add_action( 'woocommerce_order_details_after_order_table', 'wpsnippets_display_custom_order_status' );

This code checks if the order has the custom order status “Custom Status” and displays a custom message below the order table in the order details page. The get_the_ID() function retrieves the current order ID, and the wc_get_order() function fetches the order object. The order status is then checked using the get_status() method, and if it matches the custom status, the message is displayed.

Last updated on October 18, 2023. Originally posted on December 30, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *