Parent Order and Sub order status sync

– When an order is created on the woocommerce end, we create the corresponding sub-order.
– Now, we check the initial order status of the order from woocommerce’s main order and set same order status for suborders too
– After that, each vendor/admin needs to change order status for each sub-order
– Therefore, apart from initial order status, if you change status of an order, this will not affect the sub order’s status
Now, you need to add some custom code to sync the parent order status with the sub orders:

// Auto complete sub orders
add_action( 'woocommerce_order_status_completed', 'mvx_order_status_completed' );
function mvx_order_status_completed( $order_id ) {
 global $WCMp;
 $suborder_details = get_mvx_suborders($order_id);
 foreach ($suborder_details as $key => $value) {
     $suborder_fetch = array(
         'ID'           => $value->get_id(),
         'post_status'   => 'wc-completed',
     );
     wp_update_post( $suborder_fetch );
 }
}

//Auto complete sub orders to cancelled
add_action( 'woocommerce_order_status_cancelled', 'mvx_suborder_status_change_to_cancel' );
function mvx_suborder_status_change_to_cancel( $order_id ) {
   global $WCMp;
   $suborder_details = get_mvx_suborders($order_id);
   foreach ($suborder_details as $key => $value) {
       $suborder_fetch = array(
           'ID'           => $value->get_id(),
           'post_status'   => 'wc-cancelled',
       );
       wp_update_post( $suborder_fetch );
   }
}

//Auto complete sub orders to processing
add_action( 'woocommerce_order_status_processing', 'mvx_suborder_status_change_to_processing' );
function mvx_suborder_status_change_to_processing( $order_id ) {
   global $WCMp;
   $suborder_details = get_mvx_suborders($order_id);
   foreach ($suborder_details as $key => $value) {
       $suborder_fetch = array(
           'ID'           => $value->get_id(),
           'post_status'   => 'wc-processing',
       );
       wp_update_post( $suborder_fetch );
   }
}

Leave a Reply