Nik7
Nik7

Reputation: 386

WooCommerce Subscriptions count renewal orders and add to column

We are using WooCommerce Subscription and want to add a new column in the subscription overview page (/wp-admin/edit.php?post_type=shop_subscription).

We want to count all already paid renewal orders and then out put that in a column next to end date.

This is what we have so far. But it does not work

function count_paid_renewals( $subscription ) {

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

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

            ?>
                <tr>
                    <td><?php esc_html_e( 'Count: ' . $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', 'count_paid_renewals', 99, 1 );

Upvotes: 1

Views: 630

Answers (1)

Howard E
Howard E

Reputation: 5659

This will add the admin columns, and retrieve the subscription payment counts for each type and combine.

function dd_columns_head( $defaults ) {
    // add the count column after end date.
    return array_slice( $defaults, 0, 10, true ) +
        array( 'count' => 'Count' ) +
        array_slice( $defaults, 10, count( $defaults ) - 1, true );
}
function dd_column_content( $column_name, $post_ID ) {
    if ( 'count' === $column_name ) {
        $subscription = wcs_get_subscription( $post_ID );
        if ( is_a( $subscription, 'WC_Subscription' ) ) {
            // Here your subscription details are in the $subscription variable.
            $completed_count = $subscription->get_payment_count( 'completed', 'renewal' );
            $pending_count   = $subscription->get_payment_count( 'pending', 'renewal' );
            echo '<table>';
            foreach ( $subscription->get_items() as $item ) {

                ?>
                <tr>
                    <td><?php esc_html_e( 'Count: ' . $item->get_product()->get_title(), 'woocommerce-subscriptions' ); ?></td>
                    <td>
                        <?php echo absint( $completed_count + $pending_count ); ?>
                    </td>
                </tr>
                <?php
            }
            echo '</table>';

        }
    }
}
add_filter( 'manage_edit-shop_subscription_columns', 'dd_columns_head', 30 );
add_action( 'manage_shop_subscription_posts_custom_column', 'dd_column_content', 30, 2 );

Upvotes: 3

Related Questions