Show total count of expired WooCommerce subscriptions on custom admin page

49 Views Asked by At

I am using WooCommerce Subscription and I want to count all expired subscriptions then output that in a column of custom plugin's page in WordPress admin panel. This is what I have so far. But it does not work as expected. It always displays 0.

$subscription_expiry_count=0;
foreach ($users as $user_id) {
$users_subscriptions = wcs_get_users_subscriptions($user_id->id);

  if ($users_subscriptions->has_status(array('expired'))) {
         $subscription_expiry_count= $subscription_expiry_coun+1;
  }

}

echo '<div><p>Total Expired Subscriptions</p><p>'.$subscription_expiry_count.'</p>';
1

There are 1 best solutions below

0
On

There are some mistakes in your code, try the following revisited code instead:

$expired_count = 0; // Initialize
$subscribers   = get_users( array('role' => 'subscriber') ); // Get users

// Loop through subscriber users
foreach ( $subscribers as $user ) {
    $user_subscriptions = wcs_get_users_subscriptions($user->ID);

    // Loop through subscriptions
    foreach ( $user_subscriptions as $subscription ) {
        if ( $subscription->get_status() === 'expired' ) {
            $expired_count++;
        }
    }
}

echo '<div><p>Total Expired Subscriptions: '.$expired_count.'</p></div>';