Is there woocommerce function that will return me all related orders (at least the order ID) for the specific subscription that user has bough?
I have found in this official documentation Subscription Function & Property Reference:
WC_Subscription::get_related_orders( $return_fields, $order_type );
But this does not seem to be for specific subscription?
When ever I try to run it I get a fatal error no mater what I pass in:
Fatal error: Uncaught Error: Using $this when not in object context in
C:\xampp\htdocs\mysite.com\wp-content\plugins\woocommerce-subscriptions\includes\class-wc-subscription.php:1413
I am making my own plugin and I select all subscriptions where post status = wc-active
from post table. I have looked in the "woocommerce_order_items
", "woocommerce_order_itemmeta
" and "postmeta
" tables but neither of them provide way to get related orders for user bought subscription...
If I only knew where are the relation for user bought subscriptions and its related orders, then i could write some sql but I have no idea and google does not yield any results either.
Any ideas?
My setup:
- php version 7.0.4
- wordpress version 4.7.3
- woocommerce 2.6.8
- woocommerce subscriptions: 2.0.18
Updated: Added WooCommerce version 3+ Compatibility
It's very easy to get the order ID from subscription object. I am going to select, just like you, all subscriptions where 'post status' = 'wc-active'
from post table.
// Get all customers subscriptions
$customer_subscriptions = get_posts( array(
'numberposts' => -1,
// 'meta_key' => '_customer_user',
// 'meta_value' => get_current_user_id(), // Or $user_id
'post_type' => 'shop_subscription', // WC orders post type
'post_status' => 'wc-active' // Only orders with status "completed"
) );
// Iterating through each post subscription object
foreach( $customer_subscriptions as $customer_subscription ){
// The subscription ID
$subscription_id = $customer_subscription->ID
// IMPORTANT HERE: Get an instance of the WC_Subscription Object
$subscription = new WC_Subscription( $subscription_id );
// Or also you can use
// wc_get_order( $subscription_id );
// Getting the related Order ID (added WC 3+ comaptibility)
$order_id = method_exists( $subscription, 'get_parent_id' ) ? $subscription->get_parent_id() : $subscription->order->id;
// Getting an instance of the related WC_Order Object (added WC 3+ comaptibility)
$order = method_exists( $subscription, 'get_parent' ) ? $subscription->get_parent() : $subscription->order;
// Optional (uncomment below): Displaying the WC_Subscription object raw data
// echo '<pre>';print_r($subscription);echo '</pre>';
}
You can also uncomment in the post query the 'meta_key'
and 'meta_value'
array lines to get the subscriptions for one customer… This code is tested and works
The most important thing here is:
$subscription = new WC_Subscription($customer_subscription->ID);
…as you will get the WC_Subscription object in which you can apply all WC_Subscription methods without getting errors, with for example:
$subscription = new WC_Subscription($post_id);
$relared_orders_ids_array = $subscription->get_related_orders();