2

I a need to create a wordpress template to collect all Woocommerce subscriptions, but I'm having trouble with the documentation. I need to know which files to import and which function to call.

Thank you in advice.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Henrique Vilela
  • 23
  • 1
  • 1
  • 4

2 Answers2

6

Update - You can use multiple ways:

  1. The built-in function wcs_get_subscriptions() which accepts specific arguments. In your case, to get all subscriptions, you will use:

    $subscriptions = wcs_get_subscriptions(['subscriptions_per_page' => -1]);

You will get an array of Subscription protected objects where you can use on each object the WC_Data get_data() method, to access the data:

$subscriptions = wcs_get_subscriptions(['subscriptions_per_page' => -1]);

// Loop through subscriptions protected objects
foreach ( $subscriptions as $subscription ) {
    // Unprotected data in an accessible array
    $data = $subscription->get_data();
 
    print_r( $data ); // Display the subscription raw data array
}

Related: Get Subscription Product author of a Woocommerce subscription


  1. You can also use a SQL query to get all subscriptions

A SQL query can be lighter and allows more filtering. This way you can get only the required data in a more effective way.

As subscriptions are a Wordpress custom post type, You can get all subscriptions IDs first. Then in a foreach loop you will be able to get the WC_subscription object.

global $wpdb;

// get all subscriptions IDS
$subscriptions_ids = $wpdb->get_col("
    SELECT ID  FROM {$wpdb->prefix}posts 
    WHERE post_type LIKE 'shop_subscription'
");

// Loop through subscriptions Ids
foreach($subscriptions_ids as $subscription_id){
    // Get an instance of the WC_Subscription object
    $subscription = new WC_Subscription( $subscription_id );

    $data = $subscriptions->get_data();
 
    print_r( $data ); Display the subscription raw data (unprotected accessible array)
}

Then with the $subscription object and the $subscription_id you will be able to do what you want, using WC_Subscription methods to get the desired data or the using subscription ID on dedicated functions.


Official developer Documentation:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for this, I tried your solution and it brought the created subscriptions as products, what if I want to loop through user subscriptions, what users have subscribed to? For example, I have Product A and Product B all subscription products. Product B has 3 users subscribed, I want to loop through 3 users' subscriptions and not subscription products. How can that be achieved any method? Thank you – Karue Benson Karue Mar 02 '21 at 11:27
  • 1
    @KarueBensonKarue Sorry but this code goes through users subscriptions and not subscription products. – LoicTheAztec Mar 02 '21 at 11:31
  • I have only one active subscription and the plugin is printing twice in a loop. Please help me understand why. I have two subscription products but only one user subscription. Why is your code printing datat twice? If it was looping users subscriptions I would expect it to print once – Karue Benson Karue Mar 02 '21 at 12:08
  • 1
    @KarueBensonKarue See at the beginning of my answer: *"…which accepts [specific arguments](https://github.com/wp-premium/woocommerce-subscriptions/blob/master/wcs-functions.php#L425-L438)."* … You need to set an additional argument `, 'subscription_status' => 'wc-active'` or `, 'subscription_status' => 'active'` (One of the 2 is the good one)… – LoicTheAztec Mar 02 '21 at 12:25
  • 1
    Thanks for your solution actually it has helped me. I have used your code and the client is satisfied and happy. I was missing the point of wc_active. Thank you, This is the best answer – Karue Benson Karue Mar 02 '21 at 12:40
  • @KarueBensonKarue I know, just saying… – LoicTheAztec Mar 03 '21 at 14:28
3

You can use the built in function wcs_get_subscriptions($args) and pass the following $args

$args = array( 'subscriptions_per_page' => -1 );

$subscriptions = wcs_get_subscriptions( $args );

You can even filter by subscription status also in the arguments.

Andrew Schultz
  • 4,092
  • 2
  • 21
  • 44