1

I'm using WooCommerce and Subscriptions. When customers switch the items in its subscription they go through the entire order process again and the order status is set to Processing/Completed. I wanted to change the order status to Switch order. But my problem is I don't know what data I'm going to use to target only switch subscription orders.

add_action( 'woocommerce_thankyou', 'thankyou_change_order_status_to_switch' );

function thankyou_change_order_status_to_switch( $order_id ){
   if( ! $order_id ) return;

   $order = wc_get_order( $order_id );


    if ( $order->has_status( 'processing' ) ) {          
       $order->update_status( 'switch-order' );     
    }           
}
mujuonly
  • 11,370
  • 5
  • 45
  • 75
Jan Bantolinay
  • 69
  • 1
  • 12

1 Answers1

0

I managed to solved the problem. Here is the code.

add_action( 'woocommerce_thankyou', 'thankyou_change_order_status_to_switch' );

function thankyou_change_order_status_to_switch( $order_id ){
   if( ! $order_id ) return;

   $order = wc_get_order( $order_id );

if( wcs_order_contains_subscription( $order, 'switch' ) && $order->has_status( 'processing' ) ){

    $subscriptions = wcs_get_subscriptions_for_order( $order_id );
    foreach( $subscriptions as $subscription_id => $subscription ){                     
            $order->update_status( 'switch-order' );
    }
}   

}

I don't think this line of codes is still needed.

    $subscriptions = wcs_get_subscriptions_for_order( $order_id );
    foreach( $subscriptions as $subscription_id => $subscription ){}     
Jan Bantolinay
  • 69
  • 1
  • 12