Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Subscription id from the Order ID

I'm trying to get the subscription id from the action hook woocommerce_order_status_changed.

It gives me the order id which is changed every switch the customer makes.

For example: If the subscription id is 10, the original order id is 9.

Now every switch the customer made generates a new order id, which the action above gives you. At this point I have the $customer_id, $order_id, and the original post id which is 9,

How I can get the subscription id of the current order ?

Thanks

like image 742
Shlomi Avatar asked Mar 31 '17 22:03

Shlomi


People also ask

Where to find microsoft subscription ID?

Select the customer from the customer list, then select Account. On the customer's Account page, look for the Microsoft ID in the Customer Account Info section. The Microsoft ID is the same as the customer ID ( customer-tenant-id ).

How do I check my subscription status in Woocommerce?

You can try using the wcs_get_subscriptions function. First you need to track somehow which subscription the user created account with. If they are able to later signup for something else or they cancel the first and add another subscription, you will need to be able to get the first one.


1 Answers

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 
    // ...
}
like image 66
LoicTheAztec Avatar answered Oct 12 '22 18:10

LoicTheAztec