Prevent Redirect After Status Change

User
after status change for orders in woocommerce from wp admin don't redirect to all order page
Assistant
To prevent WooCommerce from redirecting to the "All Orders" page after changing an order's status in the WordPress admin, you can use a custom script to modify this behavior. Here's how you can do it:

### Steps:

1. **Add a custom JavaScript snippet**:
Use the `admin_enqueue_scripts` hook to enqueue a custom JavaScript file that modifies the behavior of the order status change dropdown.

2. **Code Implementation**:
Add the following code to your theme's `functions.php` file or a custom plugin:

add_action('admin_enqueue_scripts', function($hook) {
       // Only enqueue on the WooCommerce Orders admin page
       if ($hook === 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] === 'shop_order') {
           wp_enqueue_script('custom-order-status-script', get_template_directory_uri() . '/js/custom-order-status.js', ['jquery'], '1.0', true);
       }
   });

3. **Create the JavaScript File**:
Save the following code in a file named `custom-order-status.js` inside your theme's `js` directory (or adjust the path accordingly):

jQuery(document).ready(function ($) {
       // Override the redirect behavior after changing order status
       $(document).on('change', '.wc_order_status', function () {
           var form = $(this).closest('form');
           form.submit(function (e) {
               e.preventDefault();
               // Prevent the redirect by not allowing form submission
               alert('Order status changed successfully!');
               return false;
           });
       });
   });

4. **Alternative Method: Use a Filter**:
If you'd rather control this with a PHP-only solution, you can use the `woocommerce_redirect_to_admin_orders_page` filter. Unfortunately, WooCommerce does not directly offer this hook for redirection after status change, so this method may be limited.

add_filter('woocommerce_redirect_to_admin_orders_page', '__return_false');

5. **Test the Implementation**:
Go to the **WooCommerce Orders** page in your WordPress admin. Change the order status of an order and verify that it does not redirect to the "All Orders" page.

This solution lets you customize the behavior when changing order statuses without breaking the functionality of the admin page. Let me know if you need additional adjustments!

Leave a Comment

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