Support » Plugin: WooCommerce » Set custom order status in Woocommerce backend

  • Resolved Stu

    (@stuartiwoodheadgmailcom)


    I need to have an order status called ‘backorder’ in my WooCommerce backend. My customers pay upfront for backordered items but when scrolling down the orders page I have a bunch of ‘Processing’ orders and no way of differentiating between orders I’m waiting on goods for and orders I can fulfill immediately because I have the stock. I have successfully created the ‘Backorder’ status but I can’t find a way to trigger it to be set on new orders containing backordered goods. I can go into an order and manually change the status but it’s a little clunky.

    Here is my code to set the status up
    —————————————————————–

    function register_backorder_order_status() {
        register_post_status( 'wc-backorder-status', array(
            'label'                     => 'Backorder',
            'public'                    => true,
            'show_in_admin_status_list' => true,
            'show_in_admin_all_list'    => true,
            'exclude_from_search'       => false,
            'label_count'               => _n_noop( 'Backorder <span class="count">(%s)</span>', 'Backorders <span class="count">(%s)</span>' )
        ) );
    }
    add_action( 'init', 'register_backorder_order_status' );
    function add_backorder_to_order_statuses( $order_statuses ) {
        $new_order_statuses = array();
        foreach ( $order_statuses as $key => $status ) {
            $new_order_statuses[ $key ] = $status;
            if ( 'wc-processing' === $key ) {
                $new_order_statuses['wc-backorder-status'] = 'Backorder';
            }
        }
        return $new_order_statuses;
    }
    add_filter( 'wc_order_statuses', 'add_backorder_to_order_statuses' );

    —————————————————————–

    Nad here is how I have tried to set the new status to new orders
    —————————————————————–

    function mysite_pending($order_id) {
    
        $order = new WC_Order($order_id);
        $items = $order->get_items();
        $backorder = FALSE;
    
        foreach ($items as $item) {
            if ($item['Backordered']) {
                $backorder = TRUE;
                break;
            }
        }
        if($backorder){
            $order->update_status('backorder'); //change status here
    		$order->save();
        }
    }
    add_action('woocommerce_order_status_pending', 'mysite_pending');

    —————————————————————–

    I’m sure it’s something simple but I’ve been buried in this for days – any ideas?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Rashed Ripon

    (@rur165)

    Hello @stuartiwoodheadgmailcom ,

    This is an interesting idea.

    It looks like the first part of the code is the same almost in every example. The last part of the solution, where we update the order status of the order is a little bit tricky. I have used a different approach and hooked with woocommerce_thankyou to make it work. Here is the last part of the code –

    // ---------------------
    // 3. Set Custom Order Status @ WooCommerce Checkout Process
     
    add_action( 'woocommerce_thankyou', 'extended_thankyou_change_order_status' );
     
    function thankyou_change_order_status_extended( $order_id ){
       if( ! $order_id ) return;
       $order = wc_get_order( $order_id );
     
       // Status without the "wc-" prefix
    
       // Get a an instance of order object
       $order = wc_get_order( $order_id );
    
       foreach ( $order->get_items() as $item ) {
    
               // Get a an instance of product object related to the order item
               $product = $item->get_product();
    
               // Check if the product is on backorder
               if( $product->is_on_backorder() ){
                   // Change this order status
                   $order->update_status( 'custom-status' );
                   break; // Stop the loop
              }
        }
    }

    Please change the order status names/slug according to your use case. In case, you want to see the full code – https://gist.github.com/rashedripon/a9459f6cf771569f7bee642690f5de19

    I hope this helps.

    Thank you 🙂

    Thread Starter Stu

    (@stuartiwoodheadgmailcom)

    Really appreciate your input once again and coming at it from a different angle was a great idea.

    Unfortunately it produces the same result as my original code – new orders come through as ‘Processing’ and are not changed to our newly created status.

    Seems there are a lot of ‘old’ solutions for previous iterations of WC that have worked in the past similar to what we both tried.

    I’m hung up on the “$order->update_status( ‘custom-status’ );” line – I’ve tried set_status but same issue – just not convinced new WC version are allowing this update?

    Plugin Support Rashed Ripon

    (@rur165)

    Hello @stuartiwoodheadgmailcom ,

    I just realized I did not paste the correct function name with the hook. As a result, the order status update function was not working at all.

    Here is the fixed code of the above one –

    // ---------------------
    // 3. Set Custom Order Status @ WooCommerce Checkout Process
     
    add_action( 'woocommerce_thankyou', 'extended_thankyou_change_order_status' );
     
    function extended_thankyou_change_order_status( $order_id ){
       if( ! $order_id ) return;
       
       // Get a an instance of order object
       $order = wc_get_order( $order_id );
       
       foreach ( $order->get_items() as $item ) {
    
               // Get a an instance of product object related to the order item
               $product = $item->get_product();
    
               // Check if the product is on backorder
               if( $product->is_on_backorder() ){
                   // Change this order status
                   $order->update_status( 'custom-status' );
                   break; // Stop the loop
              }
        }
    }

    I have tested this extensively with Bank payment and Stripe payment gateway as well. It is working properly for products which are in back-order.

    I hope this time works for you.

    Thank you 🙂

    Thread Starter Stu

    (@stuartiwoodheadgmailcom)

    Genius!

    Phew – I’ve lost sleep on that one! Once again indebted to you for your time and efforts. Only trek I added was to set the loop in the drop down so the custom status isn’t at the bottom of the list (just from a workflow point of view it made sense to me to do that).

    Sure this code will help others.

    6 out of 5 stars 🙂

    …now if I can only figure out how to set a product category to only show in the backend for filtering purposes without adding yet another plug in 🙂

    Thank You!!!!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.