WooCommerce Subscriptions are widely used for recurring payments, and in some cases, store owners or developers need to change the payment amount for an active subscription. This may be necessary for proration adjustments, discounts, customer support cases, or custom business logic. In my previous article, I explained how to change the next payment date in WooCommerce Subscriptions. In this article, I am going to show how to update the payment amount and what to consider when doing so.
How to change the payment amount?
WooCommerce Subscriptions provides two different hooks depending on when the subscription is being handled. The first one runs when a subscription is created during checkout and is used for new subscriptions — woocommerce_checkout_subscription_created.
The second hook is triggered for scheduled renewal payments (excluding the initial payment) and is used to handle future billing cycles — woocommerce_scheduled_subscription_payment. Understanding the difference between these two hooks is essential when you need to control or modify the payment amount correctly.
How to change the payment amount for new subscriptions?
For example, you may want all customers who subscribe in July to have their payment amount set to 50% of the full subscription price. This can be useful for special promotions, discounts, or trial periods. In this case, we can use the woocommerce_checkout_subscription_created hook and create a custom function that adjusts the initial payment amount when the subscription is first created.
add_action('woocommerce_checkout_subscription_created', function($subscription, $order, $recurring_cart) {
// Ensure we have valid subscription and order objects
if ($subscription instanceof WC_Subscription) {
if ($order instanceof WC_Order) {
// Mark the initial order as paid
$order->payment_complete();
}
// Get current month
$number_of_month = date('n'); // numeric month (1-12)
$vacation = false;
// Check if we are in vacation period (July)
if ($number_of_month == 7) {
$vacation = true;
}
if ($vacation) {
// If vacation, set 50% discount
$order_total = $order->get_total();
$discounted_total = $order_total * 0.5; // 50% discount
$order->set_total($discounted_total);
$order->save();
}
}
}, 10, 3);
We use the woocommerce_checkout_subscription_created hook, which receives the $subscription, $order, and $recurring_cart parameters, allowing us to access the subscription object and apply custom logic at the moment the subscription is created.
$subscription – A WC_Subscription instance representing the subscription just created on checkout.
$order – A WC_Order instance representing the order for which subscriptions have been created.
$recurring_cart – A WC_Cart instance representing the cart which stores the data used for creating this subscription.
We get the current month, which we use to determine whether we are in the holiday period or not.
// Get current month
$number_of_month = date('n'); // numeric month (1-12)
Next, we check whether the current month is July, and if so, we apply the holiday logic to adjust the subscription payment amount.
// Check if we are in vacation period (July)
if ($number_of_month == 7) {
$vacation = true;
}
If the current month falls within the holiday period, we set the subscription’s payment amount to 50% of the full price. This adjustment ensures that all subscriptions created during the holiday receive the correct discounted amount.
// If vacation, set 50% discount
$order_total = $order->get_total();
$discounted_total = $order_total * 0.5; // 50% discount
$order->set_total($discounted_total);
$order->save();
How to change the payment amount for an active subscription?
In this case, we will use the same approach: if the current date falls within the defined holiday period, we update the payment amount for the active subscription accordingly.
add_action('woocommerce_scheduled_subscription_payment', function($subscription_id) {
$subscription = wcs_get_subscription($subscription_id);
if ($subscription) {
// Get current month
$number_of_month = date('n'); // numeric month (1-12)
$vacation = false;
// Check if we are in vacation period (July)
if ($number_of_month == 7) {
$vacation = true;
}
if($vacation){
try {
$order_id = $subscription->get_last_order();
if ($order_id) {
$order = wc_get_order($order_id);
if ($order) {
$order_total = $order->get_total();
$discounted_total = $order_total * 0.5; // 50% discount
$order->set_total($discounted_total);
$order->save();
}
}
} catch (\Throwable $th) {
// Catch errors silently
}
}
}
}, 10, 1);
We use the woocommerce_scheduled_subscription_payment hook, which receives only the $subscription_id parameter. This parameter represents the ID of the subscription for which a recurring renewal payment is being processed, allowing us to apply custom logic for active subscription payments.
We follow the same logic by getting today’s date and checking whether it falls within the holiday period before applying any changes to the subscription’s payment amount.
// Get current month
$number_of_month = date('n'); // numeric month (1-12)
$vacation = false;
// Check if we are in vacation period (July)
if ($number_of_month == 7) {
$vacation = true;
}
If the current month falls within the holiday period, we set the subscription’s payment amount to 50% of the full price. This adjustment ensures that all payment made during the holiday receive the correct discounted amount.
We use a try-catch block to ensure that subscription payments do not fail if any unexpected errors occur during the process.
//if now is vacation
if($vacation){
try {
$order_id = $subscription->get_last_order();
if ($order_id) {
$order = wc_get_order($order_id);
if ($order) {
$order_total = $order->get_total();
$discounted_total$order_total * 0.5; // 50% discount
$order->set_total($discounted_total);
$order->save();
}
}
} catch (\Throwable $th) {
// Catch errors silently
}
}
By using WooCommerce hooks and custom logic, you can easily adjust the payment amount for subscriptions based on specific conditions, such as seasonal promotions or special discounts. This approach ensures that your billing process remains flexible and accurate while preventing errors from affecting subscription payments. With careful handling, you can provide a smooth experience for both your store and your customers.
If you require custom subscription adjustments or additional features, contact us and we’ll be happy to support you.