Search code examples
phpwordpresswoocommercehook-woocommercestatus

Remain Woocommerce Order Status to On-Hold


I do not want Woocommerce to automatically update the order status from "On hold" to "Completed". I want it to remain as as "On hold" since we are sending a Replacement Item and we are waiting for the original item to come back to us. Basically, I want it to set as "On-hold" even after the Item has been shipped. Is there a way to achieve this?

enter image description here

I tried using the code below but no luck:

add_action( 'woocommerce_payment_complete', 'cancel_completed_status' );
   
function cancel_completed_status( $order_id ){
  $order = wc_get_order( $order_id );
  $items = $order->get_items(); 
  foreach ( $items as $item_id => $item ) {
    $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
    
    if ( $order->has_status( 'on-hold' )) {
        $order->update_status( 'on-hold' );
        $order->save();
    }
  }
}

Solution

  • add_action('woocommerce_order_status_completed', 'completed_to_onhold');
    
    function completed_to_onhold($order_id) {
    
        $order = new WC_Order($order_id);
        $order->update_status('on-hold');
    }