WooCommerce Subscriptions - Getting Related Orders Identifiers for a specific subscription

Is there a woocommerce function that will return me all related orders (at least the order identifier) ​​for a specific subscription that the user has?

I found in this official documentation the Subscription Function and the property link :

WC_Subscription::get_related_orders( $return_fields, $order_type );

But this does not look like a specific subscription?

When I try to run it, I get the no mater fatal error that I went through:

Fatal error: Crash error: using $ this if not in the context of an object in C: \ XAMPP \ HTDOCS \ mysite.com \ content-sorts \ Plugins \ WooCommerce-subscriptions \ includes \ toilet-class-subscription.php: 1413

I create my own plugin and I select all subscriptions where post status = wc-activefrom the message table. I looked at the tables " woocommerce_order_items", " woocommerce_order_itemmeta" and " postmeta", but none of them provides a way to get related orders for a user subscription ...

If I only knew where the relationship is for users who buy subscriptions and related orders, then I can write some sql, but I have no idea, and google also does not give any results.

Any ideas?

My setup:

  • php version 7.0.4
  • Wordpress version 4.7.3
  • woocommerce 2.6.8
  • woocommerce subscription: 2.0.18
+4
source share
1 answer

: ​​ WooCommerce 3+

. , , , 'post status' = 'wc-active' .

// 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>';
}

'meta_key' 'meta_value', ...

:

$subscription = new WC_Subscription($customer_subscription->ID);

... WC_Subscription, WC_Subscription, , :

$subscription = new WC_Subscription($post_id);
$relared_orders_ids_array = $subscription->get_related_orders();
+8

Source: https://habr.com/ru/post/1672275/


All Articles