Return to the order list when you update an order’s status

I was being driven mad when updating orders in my wife’s online art shop. Every time I updated an order’s status, when you hit “Update” you get sent back to the order edit screen. It makes so much more sense to be sent back to the list of orders so you can get cracking on the next one.

And so I went searching for an action to hook into that would let me do this. Woocommerce has an action that runs only for manual status updates, which is exactly what I was looking for: woocommerce_order_edit_status. BUT, there are two places where you can update the status manually:

  1. From the shop order list, using the tickboxes and dropdowns.
  2. From the edit order screen.

In order to work out where you are, or more specifically, which admin screen you’re on, you need to make use of the global $current_screen variable or use the built-in get_current_screen() function. I chose to use the latter.

The first part of the code looks like this:

function sv_man_order_status_action($order_id, $newstatus) {
    $hook = get_current_screen();
    if  ( !( $hook->id === 'shop_order' && $order_id && $newstatus )) {
        return;
    }
}

add_action('woocommerce_order_edit_status', 'sv_man_order_status_action');

The woocommerce_order_edit_status hook has 2 variables that are passed: $order_id and $new_status, both of which are pretty self-explanatory. We use an IF statement to make sure we’re on the correct admin screen, an order has actually been referenced and a status has in fact been changed. Remember this action is only triggered for a manual status change, so this code will never run during the normal operation of order status changing from pending to processed when a customer makes a payment with a credit card, for example.

The above code only gets us into the right place as far as deciding when to run. The next step is what to do when the update button is pressed. In our case we want to redirect back to the shop order list instead of seeing the edit-order screen again. WordPress provides us with just the filter to get this done: redirect_post_location. There is no need to try and run a custom wp_redirect() or something similar.

redirect_post_location only passes one variable, $location, and simply holds the URL that must be loaded when the save/update action has completed. Our final code with the new filter and function added looks like this:

function sv_man_order_status_action($order_id, $newstatus) {
    $hook = get_current_screen();
    if  ( !( $hook->id === 'shop_order' && $order_id && $newstatus )) {
        return;
    }
    
    add_filter('redirect_post_location', 'sv_man_status_redirect');  
  
}

add_action('woocommerce_order_edit_status', 'sv_man_order_status_action');

function sv_man_status_redirect($location){
    if ( !($location) ) {
        return;
    }
    
    $redirect = admin_url('edit.php?post_type=shop_order');
    return $redirect;
}

You can see the redirect filter has been inserted inside the sv_man_order_status_action() function and will only be called if the conditional statement above it is false, which for us, is when an order status has been manually changed.

I hope this has helped you customize your Woocommerce shop and get the most out of it.

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.