We are using WooCommerce Subscriptions. I've try to create a code to count and list the result from all all active subscription where end date is not before next sync date of next month. We use the Sync Subscription option always at the 1st of the month. We want to list the result directly on the subscription overview page (/wp-admin/edit.php?post_type=shop_subscription).
We have two product to count: Product ID's = 10800 AND 15340
I found that question for some ideas how this maybe can work... Get active subscriptions count for a defined product and user in Woocommerce
function get_active__subscriptions_count( $product_id ) {
global $wpdb;
// products ID's to count
$product1 = 10800
$product2 = 15340
// return the active subscriptions for a defined product ID
return $wpdb->get_var("
SELECT sum(woim2.meta_value)
FROM {$wpdb->prefix}posts as p
LEFT JOIN {$wpdb->prefix}posts AS p2 ON p.post_parent = p2.ID
LEFT JOIN {$wpdb->prefix}postmeta AS pm ON p2.ID = pm.post_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON pm.post_id = woi.order_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim2 ON woim.order_item_id = woim2.order_item_id
WHERE p.post_type LIKE 'shop_subscription' AND p.post_status LIKE 'wc-active'
AND woim.meta_key = '_product_id' AND woim.meta_value = '$product_id'
AND woim2.meta_key = '_qty'
");
}