Retrieve all affiliates related to an affiliate

Hi guys,

We’re having some issues with affiliates_get_user_affiliate. Apparently it only returns the first level of affiliates related to an user, not all affiliates. Apparently it only returns the active Affiliate ID of an user (not the Affiliate IDs from tiers, which is what I understood from the API documentation here : http://api.itthinx.com/affiliates/source-function-affiliates_get_user_affiliate.html#1611-1631)

We need to be able to retrieve an array of all affiliates in an affiliate team. Could you please help us with this?

I’ve tried to look up in the “affiliates_tiers” shortcode as it retrieve the hierarchy we’re looking for. So far though we’ve had a hard time to code a function inspired by “affiliate_tiers” that would return a simple array of the affiliates.

Thank you!

Regards,
B

#### Follow-up ####

I’ve started to put together a function that seems to be working, although we still have some bug hopefully you can help us with…

This is a background function used to retrieve the actual affiliate ids related to an affiliate (with a direct relation)
/**
* Return the affiliate ids related to an affiliate.
* @param int $affiliate_id
* @return array of int affiliate ids or null on failure
*/
function affiliates_get_user_affiliate_relations( $affiliate_id ) {
global $wpdb;
$result = null;
$affiliates_table = _affiliates_get_tablename( 'affiliates' );
$affiliates_relations_table = _affiliates_get_tablename( 'affiliates_relations' );
if ( $affiliates = $wpdb->get_results( $wpdb->prepare(
"SELECT $affiliates_table.affiliate_id FROM $affiliates_relations_table LEFT JOIN $affiliates_table ON $affiliates_relations_table.to_affiliate_id = $affiliates_table.affiliate_id WHERE $affiliates_relations_table.from_affiliate_id = %d AND $affiliates_table.status ='active'",
intval( $affiliate_id )
) ) ) {
$result = array();
foreach( $affiliates as $affiliate ) {
$result[] = $affiliate->affiliate_id;
}
}
return $result;
}

This is the function that actually sift through each all relations of an Affiliate ID to retrieve the list of affiliates in its team.

/**
* Return all affiliates IDs in an affiliate's team.
* @param int $affiliate_id
* @param array $relations
* @param int $depth
* @return multidimensional array of int affiliate ids or empty array
*/
function affiliates_get_affiliate_affiliates( $affiliate_id, $relations, $depth ) {
static $result = array();
static $depth = 3;

if(empty($relations)) {
$relations = affiliates_get_user_affiliate_relations( $affiliate_id );
}

if( $depth > 0 && count($relations) > 0 ) {
foreach($relations as $v) {
$affiliate_rel = affiliates_get_user_affiliate_relations( $v );
if(count($affiliate_rel) > 0) {
$depth--;
$buffer[$v] = affiliates_get_affiliate_affiliates( $v, $affiliate_rel, $depth);
} else {
$buffer[$v] = '';
}
$result = $buffer;
}
}
return $result;
}

Two things about this code… First of all, the depth has to be set manually. Would there be a way to detect the number of active commission levels and adjust that number automatically?

More importantly, the last iteration of the script does not return the correct value and I’m not sure why. To be honest, this is the first time I play with static variables and recursive functions. My head is about to explode right now haha…

Here are what the $result looks like when depth is set to 1, 2 and 3.


R_test (depth 1)
Array
(
[29] =>
[30] => Array
(
[29] =>
)

)


R_test (depth 2)
Array
(
[29] =>
[30] => Array
(
[31] => Array
(
[29] =>
)

)

)


R_test (depth 3)
Array
(
[29] =>
[30] => Array
(
[31] => Array
(
[33] => Array
(
[29] =>
)

)

)

)

The second “[29] => ” should not be here and as you can see when we increase the depth, the data seems fine, only the last iteration returns something weird. I believe this has something to do with the static $result variable but at this point this is beyond my current knowledge of PHP.

Waiting for your feedback!

Kind regards,
B

4 Responses to Retrieve all affiliates related to an affiliate

  1. Bastien April 1, 2016 at 2:48 pm #

    Hello Antonio,

    I dug up into the code and found a short and sweet way to achieve this. Affiliate Enterprise has a small walker function that returns a notice if the sum of all commissions set is over 1.00. It uses the “Levels” setting, so we just have to retrieve that.

    $depth = get_option( Affiliates_Multi_Tier::IXAP494, Affiliates_Multi_Tier::IXAP499 );

    Thanks for your help!

    Regards,
    B

  2. Bastien March 30, 2016 at 2:22 am #

    Fantastic Antonio!

    Thank you for this snippet. That was exactly what we were looking for.

    Would there be a way to make that a tad more clever by calculating the depth automatically?

    Is there a function that could count through the affiliate levels entries with a value different than zero and return the result?

    Kind regards,
    B

    • antonio April 1, 2016 at 8:34 am #

      Hi,
      in this function with $depth you can set a depth limit. If you want ex. get only the second depth of an affiliate, really I don’t know a clever solution. I will get the two first depth with this function, and filter the result to display only the second depth.
      Kind Regards,
      Antonio B.

  3. antonio March 25, 2016 at 11:22 am #

    Hi Bastien,
    I hope that this code can help you:

    function my_multilevel_team ( $depth ) {
    $tree = array();
    if ($user_id = get_current_user_id ()) {
    if ($affiliate_id = affiliates_get_user_affiliate ( $user_id )) {
    if (count ( $affiliate_id ) > 0) {
    Affiliates_Multi_Tier::get_tree ( $affiliate_id, $tree, false, $depth );
    }
    }
    }
    return $tree;
    }

    Kind Regards,
    Antonio B.

We use cookies to optimize your experience on our site and assume you're OK with that if you stay.
OK, hide this message.

Affiliates · Contact · Jobs · Terms & Conditions · Privacy Policy · Documentation · Downloads · Useful Plugins · My Account

Share