Introduction
WooCommerce Subscriptions is a powerful tool for recurring billing, but many SaaS-like businesses and membership sites need usage-based billing or prorated subscription charges.
In this guide, we’ll cover how to track subscription usage, calculate prorated amounts, and automate billing adjustments inside WooCommerce.
Why Usage Tracking & Prorated Billing Matter
- Fair billing: Charge customers based on actual usage.
- Seamless plan changes: Automatically handle mid-cycle upgrades/downgrades.
- SaaS-like flexibility: Offer metered or pay-as-you-go billing in WooCommerce.
- Improved retention: Transparent pricing builds customer trust.
Tools & Requirements
- WooCommerce Subscriptions
- WooCommerce (latest version)
- PHP knowledge (custom hooks & filters)
- Optional: Action Scheduler (for automated billing tasks)
Step 1: Track Subscription Usage
Create a custom meta field to track usage per subscription.
function bookwp_add_usage_meta($subscription) {
if (!$subscription->get_meta('usage')) {
$subscription->update_meta_data('usage', 0);
$subscription->save();
}
}
add_action('woocommerce_subscription_created', 'bookwp_add_usage_meta');
Now you can update usage whenever an event occurs (e.g., API call, user action).
function bookwp_update_usage($subscription_id, $units) {
$subscription = wcs_get_subscription($subscription_id);
$usage = (int) $subscription->get_meta('usage');
$subscription->update_meta_data('usage', $usage + $units);
$subscription->save();
}
Step 2: Calculate Prorated Billing
If a customer upgrades or downgrades mid-cycle, prorate their next payment.
function bookwp_prorate_amount($subscription) {
$next_payment = $subscription->get_time('next_payment');
$start = $subscription->get_time('last_payment');
$days_in_cycle = ($next_payment - $start) / DAY_IN_SECONDS;
$days_remaining = ($next_payment - time()) / DAY_IN_SECONDS;
$plan_price = $subscription->get_total();
return round(($plan_price / $days_in_cycle) * $days_remaining, 2);
}
You can apply this during a subscription switch to charge only the prorated difference.
Step 3: Automate Billing Adjustments
Hook into woocommerce_scheduled_subscription_payment
to adjust charges:
add_action('woocommerce_scheduled_subscription_payment', 'bookwp_adjust_subscription_payment', 10, 2);
function bookwp_adjust_subscription_payment($amount, $subscription) {
$usage = (int) $subscription->get_meta('usage');
$rate_per_unit = 0.50; // Example rate
$extra_charges = $usage * $rate_per_unit;
return $amount + $extra_charges;
}
Step 4: Display Usage in My Account
Show customers their current usage:
add_action('woocommerce_subscription_details_table_after', function($subscription) {
$usage = $subscription->get_meta('usage') ?: 0;
echo "<p><strong>Usage this cycle:</strong> {$usage} units</p>";
});
Advanced Enhancements
- Webhook integration: Track usage automatically from an external service (e.g., SaaS API).
- Partial refunds: Automatically refund unused subscription time for downgrades.
- Custom admin UI: Add a usage editor in the admin subscription screen.
- Reports: Generate usage-based revenue analytics for admin dashboards.
Best Practices
- Always log usage updates for audit purposes.
- Validate usage inputs to prevent incorrect billing.
- Provide clear breakdowns of charges to customers.
- Test on a staging site before going live.