Shane
Shane

Reputation: 558

Get WooCommerce Subscription Original (Initial) order number

I am working on a project where a user would purchase a subscription and when they do we send data to another server using API calls. Once the server receives the necessary information it creates a serial number and sends it back to the woocommerce site.

This all works just fine, I am successful in sending data and retrieving serial numbers and synchronizing most things on the server.

I am stuck at when Woo Subscriptions renews their order I need to update information on the other server regarding the serial number. I think I would be fine if I could get access to the original order number.

The other issue I am running into is determining if the order is in fact a renewal order, I have a trivial flag set up that checks if "subscription_interval" is inside of the order->get_items, if not then its a renewal order. Something is just fishy about this whole thing.

Basically I need a way to find out if it is a renewal order and if it is give me the initial order number.

Looking at the order screen on the very bottom of the metaboxes (advanced) there is a metabox that shows "Related Subscription Orders" and even shows the initial order. How can I access this data?

Thanks in advance!

Upvotes: 0

Views: 2215

Answers (2)

Jose
Jose

Reputation: 322

Maybe this can help someone else looking for a way to find out the original order ID by the subscription ID.

function my_get_original_order_id_by_subscription_id( $sub_id ){
    $sub_post = get_post( $sub_id );
    if( $sub_post && is_object( $sub_post ) && isset( $sub_post->post_parent ) && absint( $sub_post->post_parent ) > 0 ){
        return $sub_post->post_parent;
    }
    return false;
}

The solution is very simple. You need just to get the object post related to the subscription ID ($sub_id), and then the post_parent is the ID of the original order that you were looking for.

Upvotes: 1

Shane
Shane

Reputation: 558

Ok, so I found WC_Subscriptions_Renewal_Order and ran a get_class_methods on it. I found is_renewal and get_parent_order_id, life is good again :)

Upvotes: 2

Related Questions