Home / eCommerce / AffiliateWP Network Stats Shortcode
Duplicate Snippet

Embed Snippet on Your Site

AffiliateWP Network Stats Shortcode

Adds the [affwp_network_stats] shortcode for displaying affiliate network stats (own sales, downline sales, earnings, referral counts) with shipping excluded from all sales figures. Requires AffiliateWP and the Multi-Tier Commissions add-on.

Code Preview
php
<?php
/**
 * Helper: sum product-only sales (excludes shipping) for one affiliate.
 * Filters to WooCommerce context so the dataset matches the SALES table.
 */
function affwp_product_only_sales( $affiliate_id, $status_array ) {
    $total     = 0.0;
    $referrals = affiliate_wp()->referrals->get_referrals( array(
        'affiliate_id' => absint( $affiliate_id ),
        'status'       => $status_array,
        'context'      => 'woocommerce',
        'number'       => -1,
    ) );
    if ( ! is_array( $referrals ) ) {
        return 0.0;
    }
    foreach ( $referrals as $referral ) {
        $products = maybe_unserialize( $referral->products );
        if ( ! is_array( $products ) ) {
            continue;
        }
        foreach ( $products as $product ) {
            $total += isset( $product['price'] ) ? (float) $product['price'] : 0.0;
        }
    }
    return $total;
}
/**
 * Shortcode: [affwp_network_stats]
 *
 * Returns a single network stat for the logged-in affiliate.
 * Use multiple instances in JetEngine with your own labels.
 *
 * Where to add: WPCode > Add Snippet > PHP Snippet, then activate.
 * Requires: AffiliateWP + Multi-Tier Commissions add-on.
 *
 * Attributes:
 *   type   = total_sales (default) | own_sales | downline_sales
 *            | total_earnings | own_earnings | downline_earnings
 *            | network_count | own_referrals | downline_referrals
 *   status = all (default) | paid | unpaid
 *            (status is ignored for 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_sales',
            'status' => 'all',
        ),
        $atts,
        'affwp_network_stats'
    );
    $type         = sanitize_key( $atts['type'] );
    $status       = sanitize_key( $atts['status'] );
    $status_array = ( 'all' === $status ) ? array( 'paid', 'unpaid' ) : array( $status );
    if ( 'own_sales' === $type ) {
        return affwp_currency_filter( affwp_format_amount( affwp_product_only_sales( $affiliate_id, $status_array ) ) );
    }
    if ( 'own_earnings' === $type ) {
        $total = 0.0;
        if ( in_array( 'paid', $status_array, true ) ) {
            $total += (float) affwp_get_affiliate_earnings( $affiliate_id, false );
        }
        if ( in_array( 'unpaid', $status_array, 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( 'paid', $status_array, true ) ) {
            $count += (int) affwp_count_referrals( $affiliate_id, 'paid' );
        }
        if ( in_array( 'unpaid', $status_array, true ) ) {
            $count += (int) affwp_count_referrals( $affiliate_id, 'unpaid' );
        }
        return $count;
    }
    $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 );
    }
    if ( 'downline_referrals' === $type ) {
        $count = 0;
        foreach ( $downline_ids as $id ) {
            if ( in_array( 'paid', $status_array, true ) ) {
                $count += (int) affwp_count_referrals( $id, 'paid' );
            }
            if ( in_array( 'unpaid', $status_array, true ) ) {
                $count += (int) affwp_count_referrals( $id, 'unpaid' );
            }
        }
        return $count;
    }
    $ids_to_sum = in_array( $type, array( 'downline_sales', 'downline_earnings' ), true )
        ? $downline_ids
        : $all_ids;
    if ( in_array( $type, array( 'total_sales', 'downline_sales' ), true ) ) {
        $total = 0.0;
        foreach ( $ids_to_sum as $id ) {
            $total += affwp_product_only_sales( $id, $status_array );
        }
        return affwp_currency_filter( affwp_format_amount( $total ) );
    }
    $total = 0.0;
    foreach ( $ids_to_sum as $id ) {
        if ( in_array( 'paid', $status_array, true ) ) {
            $total += (float) affwp_get_affiliate_earnings( $id, false );
        }
        if ( in_array( 'unpaid', $status_array, 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