Skip to content
Advertisement

Display subscription renewal count on WooCommerce “My account” page

We use WooCommerce subscription and I created that snippet below to display the renewal count on MyAccount page.

This is working. But, I found out, that the initial parent order also count as a “paid”. So actually I only want to count the renewals from a subscription means exclude the initial order from that count.

I guess when I always calc -1 like $get_paid_count -1 then it works. But that solution is not very clean. Any suggestions how I can do that in a clean way?

function show_paid_renewals( $subscription ) {

    $get_paid_count = $subscription->get_payment_count();

    foreach ( $subscription->get_items() as $item ) {

            ?>
                <tr>
                    <td><?php esc_html_e( 'Erhaltene Lieferungen: ' . $item->get_product()->get_title(), 'woocommerce-subscriptions' ); ?></td>
                    <td>
                        <?php echo esc_html( $get_paid_count ); ?>
                    </td>
                </tr>
                <?php
            }
    }

add_action( 'woocommerce_subscription_before_actions', 'show_paid_renewals', 99, 1 );

Advertisement

Answer

If you want to count the renewals of a subscription, which means you exclude the first order from that count. You can use:

$get_paid_count = $subscription->get_payment_count( 'completed', 'renewal' );

Subscription Function & Property Reference

WC_Subscription::get_payment_count() – Get the number of payments completed for a subscription.

Completed payments include all renewal orders and potentially an initial order (if the subscription was created as a result of a purchase from the front end rather than manually by the store manager).

Usage

<?php WC_Subscription::get_payment_count( $payment_type, $order_types ) ?>

Parameters

$payment_type

(string) (optional) Type of count ('completed'|'refunded'|'net'). Default completed.

$order_types

(array|string) (optional) Type of order relation(s) to count.'parent' and/or 'renewal'. Multiple order type values can also be provided in an array. Default array( 'parent', 'renewal' ).

Return Values

(int) The total number of related orders marked with a status representing a 'completed'|'refunded'|'net' payment depending on the first parameter.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement