Skip to content

Instantly share code, notes, and snippets.

@keefyhub
Last active February 14, 2018 12:59
Show Gist options
  • Select an option

  • Save keefyhub/b5bf2866fd8359e78a88495ca409604b to your computer and use it in GitHub Desktop.

Select an option

Save keefyhub/b5bf2866fd8359e78a88495ca409604b to your computer and use it in GitHub Desktop.
// Based on https://stackoverflow.com/questions/39596477/woocommerce-get-active-subscriptions-in-a-list-between-start-end-date
// This will remove duplicates and only return unique customers
function active_subscription_list($from_date = null, $to_date = null)
{
// Get all customer orders
$subscriptions = get_posts([
'numberposts' => -1,
'post_type' => 'shop_subscription', // Subscription post type
'post_status' => 'wc-active', // Active subscriptions. could also use wc-completed, wc-pending, wc-pending-cancel, wc-cancelled
'orderby' => 'post_date', // ordered by date
'order' => 'ASC',
'date_query' => [ // Start & end date
[
'after' => $from_date,
'before' => $to_date,
'inclusive' => true
],
]
]);
$results = [];
foreach ($subscriptions as $subscription) {
$subscr_meta_data = get_post_meta($subscription->ID);
$customer_name = $subscr_meta_data['_billing_first_name'][0] . ' ' . $subscr_meta_data['_billing_last_name'][0];
$customer_email = $subscr_meta_data['_billing_email'][0];
$subscription_status = $subscription->post_status;
array_push($results, [
'name' => $customer_name,
'email' => $customer_email,
'status' => $subscription_status
]);
}
$results = array_unique($results, SORT_REGULAR);
echo "<table class='shop_table subscription_list'>
<tr>
<th>User Name</th>
<th>User Email</th>
<th>Subscription Status</th>
</tr>
";
foreach ($results as $result) {
$name = $result['name'];
$email = $result['email'];
$status = $result['status'];
echo "</tr>
<td>$name</td>
<td>$email</td>
<td>$status</td>
</tr>";
}
echo '</table>';
}
$from_date = '2010-06-19';
$to_date = '2017-09-21';
var_dump(active_subscription_list($from_date, $to_date));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment