Home / eCommerce / AffiliateWP Network Stats Shortcode
Duplicate Snippet

Embed Snippet on Your Site

AffiliateWP Network Stats Shortcode

Adds an [affwp_network_stats] shortcode with type and status attributes to display individual network metrics for JetEngine dashboards. Requires AffiliateWP + Multi-Tier Commissions.

Code Preview
php
<?php
/**
 * Shortcode: [affwp_network_stats]
 *
 * Returns a single network stat for the logged-in affiliate.
 * Use multiple instances in JetEngine with your own labels.
 *
 * Attributes:
 *   type   = total_earnings (default) | own_earnings | downline_earnings
 *            | network_count | own_referrals | downline_referrals
 *   status = all (default) | paid | unpaid
 *            (status is ignored for network_count)
 *
 * Examples:
 *   [affwp_network_stats type="total_earnings"]
 *   [affwp_network_stats type="own_earnings" status="paid"]
 *   [affwp_network_stats type="network_count"]
 */
function affwp_network_stats_shortcode( $atts ) {
    if ( ! function_exists( 'affwp_get_affiliate_id' ) ) {
        return '';
    }
    if ( ! function_exists( '\AffiliateWP\MTC\affiliate_wp_mtc' ) ) {
        return '';
    }
    $affiliate_id = affwp_get_affiliate_id();
    if ( ! $affiliate_id ) {
        return '';
    }
    $atts = shortcode_atts(
        array(
            'type'   => 'total_earnings',
            'status' => 'all',
        ),
        $atts,
        'affwp_network_stats'
    );
    $type   = sanitize_key( $atts['type'] );
    $status = sanitize_key( $atts['status'] );
    // --- Own-only metrics (no network traversal needed) ---
    if ( 'own_earnings' === $type ) {
        $total = 0.0;
        if ( in_array( $status, array( 'paid', 'all' ), true ) ) {
            $total += (float) affwp_get_affiliate_earnings( $affiliate_id, false );
        }
        if ( in_array( $status, array( 'unpaid', 'all' ), true ) ) {
            $total += (float) affwp_get_affiliate_unpaid_earnings( $affiliate_id, false );
        }
        return affwp_currency_filter( affwp_format_amount( $total ) );
    }
    if ( 'own_referrals' === $type ) {
        $count = 0;
        if ( in_array( $status, array( 'paid', 'all' ), true ) ) {
            $count += (int) affwp_count_referrals( $affiliate_id, 'paid' );
        }
        if ( in_array( $status, array( 'unpaid', 'all' ), true ) ) {
            $count += (int) affwp_count_referrals( $affiliate_id, 'unpaid' );
        }
        return $count;
    }
    // --- Network metrics (requires full downline traversal) ---
    $all_ids = array();
    $queue   = array( $affiliate_id );
    $visited = array();
    while ( ! empty( $queue ) ) {
        $current = array_shift( $queue );
        if ( in_array( $current, $visited, true ) ) {
            continue;
        }
        $visited[] = $current;
        $all_ids[] = $current;
        $sub_ids   = \AffiliateWP\MTC\affiliate_wp_mtc()->network->get_sub_affiliates( $current );
        foreach ( $sub_ids as $sub_id ) {
            $queue[] = $sub_id;
        }
    }
    $downline_ids = array_diff( $all_ids, array( $affiliate_id ) );
    if ( 'network_count' === $type ) {
        return count( $downline_ids );
    }
    $ids_to_sum = ( 'downline_earnings' === $type ) ? $downline_ids : $all_ids;
    if ( 'downline_referrals' === $type ) {
        $count = 0;
        foreach ( $downline_ids as $id ) {
            if ( in_array( $status, array( 'paid', 'all' ), true ) ) {
                $count += (int) affwp_count_referrals( $id, 'paid' );
            }
            if ( in_array( $status, array( 'unpaid', 'all' ), true ) ) {
                $count += (int) affwp_count_referrals( $id, 'unpaid' );
            }
        }
        return $count;
    }
    // Sum earnings for total_earnings or downline_earnings.
    $total = 0.0;
    foreach ( $ids_to_sum as $id ) {
        if ( in_array( $status, array( 'paid', 'all' ), true ) ) {
            $total += (float) affwp_get_affiliate_earnings( $id, false );
        }
        if ( in_array( $status, array( 'unpaid', 'all' ), true ) ) {
            $total += (float) affwp_get_affiliate_unpaid_earnings( $id, false );
        }
    }
    return affwp_currency_filter( affwp_format_amount( $total ) );
}
add_shortcode( 'affwp_network_stats', 'affwp_network_stats_shortcode' );

Comments

Add a Comment