Get the Subscription id from the Order ID Get the Subscription id from the Order ID wordpress wordpress

Get the Subscription id from the Order ID


You can use dedicated function wcs_get_subscriptions_for_order() which will retrieve $subscription IDs.

So this could be your code:

add_action('woocommerce_order_status_changed', 'action_order_status_changed');function action_order_status_changed( $order_id ){    $subscriptions_ids = wcs_get_subscriptions_for_order( $order_id, array( 'order_type' => 'any' ) );    // We get all related subscriptions for this order    foreach( $subscriptions_ids as $subscription_id => $subscription_obj )        if($subscription_obj->order->id == $order_id) break; // Stop the loop    // The subscription ID: $subscription_id     // The An instance of the Subscription object: $subscription_obj     // ...}