WooCommerce Subscriptions: Custom Renewal Logic with wcs_get_subscriptions

Custom renewal rules in WooCommerce Subscriptions

WooCommerce Subscriptions creates a renewal order on each billing date, charges it and keeps the subscription active. Business rules beyond that, such as a loyalty discount after the twelfth payment, skipping a renewal while a customer’s account is on hold, or adding a free gift every sixth box, are added with the plugin’s hooks. The important part is choosing the hook that runs at the right moment: before the renewal order is charged, or after the payment succeeded.

Problem: A coffee subscription shop wants three rules: 10% off every renewal after the twelfth paid one, a free sample product in every sixth box, and no charge at all while a customer’s account is flagged as “paused” in the CRM. The first attempt changed the subscription’s price permanently, which also changed all later renewals.

Solution: Modify the renewal order, not the subscription, in wcs_renewal_order_created: apply the discount and add the gift item to that one order and recalculate totals. Skip renewals of paused accounts by putting the subscription on hold before payment in woocommerce_scheduled_subscription_payment at an early priority. Count paid renewals with get_payment_count().

Discount and gift on the renewal order

add_filter( 'wcs_renewal_order_created', 'ha_adjust_renewal_order', 10, 2 );
function ha_adjust_renewal_order( WC_Order $renewal, WC_Subscription $subscription ): WC_Order {
    $paid = (int) $subscription->get_payment_count( 'completed' );

    if ( $paid >= 12 ) {
        $discount = new WC_Order_Item_Fee();
        $discount->set_name( __( 'Loyalty discount (10%)', 'textdomain' ) );
        $discount->set_amount( -0.10 * (float) $renewal->get_subtotal() );
        $discount->set_total( -0.10 * (float) $renewal->get_subtotal() );
        $discount->set_tax_status( 'none' );
        $renewal->add_item( $discount );
    }

    if ( ( $paid + 1 ) % 6 === 0 ) {
        $gift = wc_get_product( (int) get_option( 'ha_gift_product_id' ) );
        if ( $gift && $gift->is_in_stock() ) {
            $item_id = $renewal->add_product( $gift, 1, array( 'subtotal' => 0, 'total' => 0 ) );
            wc_add_order_item_meta( $item_id, '_ha_gift', 'yes' );
        }
    }

    $renewal->calculate_totals();
    $renewal->add_order_note( sprintf( 'Renewal rules applied after %d paid renewals.', $paid ) );
    return $renewal;
}

wcs_renewal_order_created is a filter that runs right after the renewal order is created from the subscription and before the payment gateway charges it (the gateway’s callback runs later on the scheduled payment action). Changes here apply to this one order: the subscription’s own line items and price stay untouched, so the next renewal starts from the normal price again. get_payment_count( 'completed' ) counts successful payments of the subscription, including the parent order. A negative fee is the simplest way to discount an order programmatically; a coupon applied to the order with $renewal->apply_coupon() is the alternative when the discount should appear as a coupon in reports.

Skip renewals for paused accounts

add_action( 'woocommerce_scheduled_subscription_payment', 'ha_pause_before_renewal', 0 );
function ha_pause_before_renewal( int $subscription_id ): void {
    $subscription = wcs_get_subscription( $subscription_id );
    if ( ! $subscription ) {
        return;
    }
    if ( 'paused' === get_user_meta( $subscription->get_user_id(), 'ha_crm_status', true ) ) {
        $subscription->update_status( 'on-hold', __( 'Account paused in CRM, renewal skipped.', 'textdomain' ) );
    }
}

The scheduled payment action is processed by Action Scheduler at the renewal date. Subscriptions creates the renewal order in its own callback on the same action at priority 1, and only for active subscriptions, so this check runs at priority 0, before it. Verify the priority against the installed version of the extension when upgrading. When the account is active again, reactivating the subscription ($subscription->update_status( 'active' )) schedules the next payment from the existing dates.

Test without waiting a month

In WooCommerce → Status → Scheduled Actions, search for woocommerce_scheduled_subscription_payment and the subscription ID, and click “Run”. With a test payment gateway, the renewal order is created and charged immediately, and the order notes show which rules applied.

NOTE: These hooks belong to the WooCommerce Subscriptions extension; WooCommerce core has no subscriptions. For the general idea of reacting to order states in WooCommerce: WooCommerce order status transitions.